我有一个 Types 项目,我在其中定义了我想在我的主应用程序中处理的自定义类对象。对象基本上是从字符串派生的,并被解析成一个结构。
我有两个问题
1 - 在一个单独的项目中,我有一个文件阅读器类,我在其中扫描文本文件以查找我定义的字符串类型。例如通过正则表达式。目前,我将我的 Types 项目添加为项目参考,我只是在我的阅读类顶部列出了正则表达式。当我找到一种类型时,我将字符串转换为适当的类型。但是,我该如何改进它,使其直接连接到我的 Types 项目 - 所以当我用新类型更新它时,Read 类知道它应该支持新类型?
2 - 在从文本文件中读取这些特定类型后,我正在尝试创建一个适用于这些特定类型的 DLL。如何告诉我的 DLL 我想要支持我的 Types 项目中的类型?我是否必须为要处理的每种类型创建一个重载函数?我使用接口吗?
任何意见是极大的赞赏。
编辑:添加了我正在尝试做的示例代码
//PROJECT 1 - 处理 IO 操作,例如读写
//read 类作业中的函数是通过正则表达式查找几种预定义的字符串类型之一...一旦找到它们被转换为数据结构(通过将字符串传递给在其他项目中定义的类型类
public class Read
{
public string[] FileList { get; set; }
private static Int64 endOffset = 0;
private FileStream readStream;
private StreamReader sr;
private System.Text.RegularExpressions.Regex type1 = new System.Text.RegularExpressions.Regex(@"@123:test");
private System.Text.RegularExpressions.Regex type2 = new System.Text.RegularExpressions.Regex(@"TESTTYPE2");
public Read(string[] fl)
{
FileList = fl;
}
public object ReturnMessage(FileStream readStream, out int x)
{
//readStream = new FileStream(file, FileMode.Open, FileAccess.Read);
x = 0;
//endOffset = 0;
bool found = false;
char ch;
string line = string.Empty;
object message = null;
while (!(x < 0)) //do this while not end of line (x = -1)
{
readStream.Position = endOffset;
//line reader
while (found == false) //keep reading characters until end of line found
{
x = readStream.ReadByte();
if (x < 0)
{
found = true;
break;
}
// else if ((x == 10) || (x == 13))
if ((x == 10) || (x == 13))
{
ch = System.Convert.ToChar(x);
line = line + ch;
x = readStream.ReadByte();
if ((x == 10) || (x == 13))
{
ch = System.Convert.ToChar(x);
line = line + ch;
found = true;
}
else
{
if (x != 10 && (x != 13))
{
readStream.Position--;
}
found = true;
}
}
else
{
ch = System.Convert.ToChar(x);
line = line + ch;
}
}//while - end line reader
//examine line (is it one of the supported types?)
if (type1.IsMatch(line))
{
message = line;
endOffset = readStream.Position;
break;
}
else
{
endOffset = readStream.Position;
found = false;
line = string.Empty;
}
}//while not end of line
return message;
}
}
//PROJECT 2 - 包含定义类型的类
//TYPE1
namespace MessageTypes.Type1
{
public sealed class Type1
{
public List<Part> S2 { get; set; }
public Type1(string s)
{
S2 = new List<Part>();
string[] parts = s.Split(':');
for (int i = 0; i < parts.Length; i++)
{
S2.Add(new Part(parts[i]));
}
}
}
public sealed class Part
{
public string P { get; set; }
public Part(string s)
{
P = s;
}
}
}
//类型2
namespace MessageTypes.Type2
{
public sealed class FullString
{
public string FS { get; set; }
public FullString(string s)
{
FS = s;
}
}
}
//项目 3
class DoSomethingToTypeObject{
//detect type and call appropriate function to process
}
//项目 4 -- 带有 GUI 的主项目
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (tabControl1.SelectedIndex == 0) //Processing Mode 1
{
//load file list from main window - Mode1 tab
IOHandler.Read read = new IOHandler.Read(new string[2] { @"C:\file1.txt", @"C:\file2.txt" });
//read files
foreach (string file in read.FileList)
{
//while not end of stream
myobject = read.ProcessFile(file);
DoSomethingtoTypeObject DS = new DoSomethingtoTypeObject(myobject);
//write transoformed object
write(myobject);
}
}
}
}