C# 反映未知子类型

发布于 2024-12-13 12:46:15 字数 686 浏览 0 评论 0原文

我有一个类:

public class Chid1 : Parent{
    public string var1{get;set;}
    public string var2{get;set;}
}

public class Chid2 : Parent{
    public string var3{get;set;}
    public string var4{get;set;}
}

我正在从 xml 文件收集一些数据,我需要设置这些类。

所以这就是我现在可以做的:

string child = "Chid1";//from the xml
Parent instance = (Parent)Activator.CreateInstance(Type.GetType(child ) , ...);

由于 Parent 不包含有关子项的详细信息,我无法执行以下操作:

instance.var1 = "Some text from the XML";

这里是否有任何解决方案可以通过以下字符串设置子项:

Activator.SetParan(instance,"var1" , "Some text from the XML");

或者其他什么?

I have a class:

public class Chid1 : Parent{
    public string var1{get;set;}
    public string var2{get;set;}
}

public class Chid2 : Parent{
    public string var3{get;set;}
    public string var4{get;set;}
}

I am collecting some data from an xml file and I need to setup these classes.

So this is what I can do for now:

string child = "Chid1";//from the xml
Parent instance = (Parent)Activator.CreateInstance(Type.GetType(child ) , ...);

Since Parent does not containg details about the childs, I cant do:

instance.var1 = "Some text from the XML";

Is there any solution here of setting up the childs via strings like:

Activator.SetParan(instance,"var1" , "Some text from the XML");

Or something else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撑一把青伞 2024-12-20 12:46:15

您是否考虑过在这里使用dynamic。这将使代码变得更加简单。

dynamic instance = Activator.CreateInstance(Type.GetType(child ) , ...);
instance.var1 = "some text from the XML";

此外,您不使用已知的序列化引擎还有一个原因。如果您只是将对象序列化为 XML 流中的对象,有许多现有的解决方案可以让您的生活更轻松

Have you considered using dynamic here. It would make the code much simpler.

dynamic instance = Activator.CreateInstance(Type.GetType(child ) , ...);
instance.var1 = "some text from the XML";

Additionally is there a reason you're not using a known serialization engine. If you are simply serializing objects to an from an XML stream there are a number of existing solutions that would make your life easier

断念 2024-12-20 12:46:15

为子类提供一个构造函数,然后通过第二个参数向 Activator.CreateInstance 提供参数,该实例将构造函数参数作为参数 object[]:

 public class Child1 : Parent
 {     
       public Child1(string _var1, string _var2) 
       {
            this.var1 = _var1;
            this.var2 = _var2
       }

       public string var1 { get; set;}     
       public string var2 { get; set;} 
 }

 Activator.CreateInstance(typeof(Child1), new object[] { "var1", "var2" }); 

Give the child classes a constructor and then supply the arguments through the second argument to Activator.CreateInstance, which takes constructor args as params object[]:

 public class Child1 : Parent
 {     
       public Child1(string _var1, string _var2) 
       {
            this.var1 = _var1;
            this.var2 = _var2
       }

       public string var1 { get; set;}     
       public string var2 { get; set;} 
 }

 Activator.CreateInstance(typeof(Child1), new object[] { "var1", "var2" }); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文