This may be way out in left field, crazy, but I just need to ask before I go on implementing this massive set of classes.
这可能是在左边的领域,疯了,但我只需要在我继续实现这个庞大的类集之前询问。
Basically, I'm writing a binary message parser that decodes a certain military message format into an object. The problem is that there are literally hundreds of different message types and they share almost nothing in common with each other. So the way I'm planning to implement this is to create hundreds of different objects.
基本上,我正在编写一个二进制消息解析器,它将某种军事消息格式解码为一个对象。问题在于,实际上有数百种不同的消息类型,它们几乎没有任何共同点。所以我计划实现这个的方法是创建数百个不同的对象。
However, even though the message attributes share nothing in common, the method for decoding them is fairly straightforward and follows a pattern. So I'm planning to write a code generator to generate all the objects and the decode logic for each message type.
但是,即使消息属性没有共同之处,解码它们的方法也相当简单并遵循一种模式。所以我打算编写一个代码生成器来为每种消息类型生成所有对象和解码逻辑。
What would be really sweet is if there was some way to dynamically create an object based on some schema. It doesn't necessarily have to be XML, but XML is pretty easy to work with.
如果有某种方法可以根据某种模式动态创建对象,那真正令人满意的是什么。它不一定是XML,但XML很容易使用。
Is this possible in C#?
这可能在C#中吗?
I would like the interface to look something like this:
我希望界面看起来像这样:
var decodedMessage = MessageDecoder.Decode(byteArray);
Where the MessageDecoder figures out what type of message it is and then returns the appropriate object. It will probably return an interface which implements a MessageType Property or something like that.
MessageDecoder在哪里找出它是什么类型的消息然后返回适当的对象。它可能会返回一个实现MessageType属性的接口或类似的东西。
Basically what I'm wondering is if there is a way to have one object called Message, which implements a MessageType Property. And then Depending on the MessageType, the Message object transforms into whatever type of message it is, so I don't have to spend the time creating all of these message types.
基本上我想知道的是,是否有一种方法可以有一个名为Message的对象,它实现了一个MessageType属性。然后,根据MessageType,Message对象将转换为任何类型的消息,因此我不必花时间创建所有这些消息类型。
3 个解决方案
#1
3
ExpandOobject Where you can dynamically add fields to an object.
ExpandOobject可以向对象动态添加字段的位置。
A good starting point is here.
这里有一个很好的起点。
#2
2
Is xsd.exe what you are looking for? It can take an XML file or a schema and generate the c# classes. One problem that you might encounter though is that some of the military message formats are VERY obtuse. You could end up with some very large code files.
你正在寻找xsd.exe吗?它可以采用XML文件或模式并生成c#类。您可能遇到的一个问题是某些军事信息格式非常钝。你最终可能会得到一些非常大的代码文件。
#3
1
Look at T4 templates. They let you write code to generate code, they are integrated into the IDE, and they are quite easy really.
看看T4模板。它们允许您编写代码来生成代码,它们集成到IDE中,它们非常简单。
EDIT: There is no way to do what you are after with var
, because var
requires the right-hand side of the assignment to be statically typed (at compile time). I suppose that you could dynamically generate that statement, then compile and run it, but that's a very painful approach.
编辑:使用var之后无法执行您的操作,因为var要求分配的右侧是静态类型(在编译时)。我想你可以动态生成那个语句,然后编译并运行它,但这是一个非常痛苦的方法。
If you have XSD's for all of the message types, then you can use xsd.exe
as @jle suggests. If not, then I am curious about the following:
如果您有所有消息类型的XSD,那么您可以使用xsd.exe作为@jle建议。如果没有,那么我对以下内容感到好奇:
// Let's assume this works
var decodedMessage = MessageDecoder.Decode(byteArray);
// Now what? I don't know what properties there are on decodedMessage, so I cant do anything with it.
#1
3
ExpandOobject Where you can dynamically add fields to an object.
ExpandOobject可以向对象动态添加字段的位置。
A good starting point is here.
这里有一个很好的起点。
#2
2
Is xsd.exe what you are looking for? It can take an XML file or a schema and generate the c# classes. One problem that you might encounter though is that some of the military message formats are VERY obtuse. You could end up with some very large code files.
你正在寻找xsd.exe吗?它可以采用XML文件或模式并生成c#类。您可能遇到的一个问题是某些军事信息格式非常钝。你最终可能会得到一些非常大的代码文件。
#3
1
Look at T4 templates. They let you write code to generate code, they are integrated into the IDE, and they are quite easy really.
看看T4模板。它们允许您编写代码来生成代码,它们集成到IDE中,它们非常简单。
EDIT: There is no way to do what you are after with var
, because var
requires the right-hand side of the assignment to be statically typed (at compile time). I suppose that you could dynamically generate that statement, then compile and run it, but that's a very painful approach.
编辑:使用var之后无法执行您的操作,因为var要求分配的右侧是静态类型(在编译时)。我想你可以动态生成那个语句,然后编译并运行它,但这是一个非常痛苦的方法。
If you have XSD's for all of the message types, then you can use xsd.exe
as @jle suggests. If not, then I am curious about the following:
如果您有所有消息类型的XSD,那么您可以使用xsd.exe作为@jle建议。如果没有,那么我对以下内容感到好奇:
// Let's assume this works
var decodedMessage = MessageDecoder.Decode(byteArray);
// Now what? I don't know what properties there are on decodedMessage, so I cant do anything with it.