如何在运行时创建两个嵌套类 [英] How can Create two nested class in runtime

查看:70
本文介绍了如何在运行时创建两个嵌套类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



i有一个带有这种结构的xml文件

 <   >  
< id > 0 < / id >
< name > John < / name >
< link > http://test.com < span class =code-keyword>< / link >
< / graph >
< 图表 >
< id > 1 < span class =code-keyword>< / id >
< 名称 > Roger < span class =code-keyword>< / name >
< link > http: //test2.com< / link >
< / graph >





i想要使用Reflection.Emit类创建两个类

优先:

  A {
int id;
string name;
string link
}



秒:

 B类{
列表< A>图;
}







i阅读本文(使用Reflection.Emit创建动态类型的简介 [ ^ ])并且可以在运行时创建A类但问题在另一个运行时类(B)中使用这个(A)并且还有一个A的列表。





 MyClassBuilder MCB =  new  MyClassBuilder(  A ); 
var myclass = MCB.CreateObject( new string [ 3 ] { id name link}, new 类型[ 3 ] { typeof int ), typeof string ), typeof string )});
类型TP = myclass.GetType();
MyClassBuilder MCB1 = new MyClassBuilder( B );
// 这是我的混淆类型(List< TP>)??????发生错误
var myclass2 = MCB1.CreateObject( new string [ 1 ] { graphs}, new 类型[ 1 ] { typeof运算(列表与LT; TP>)});
// A类和工作的设定值
object tp = Activator.CreateInstance(TP);
TP.GetProperty( id)。SetValue(tp, 0 );
TP.GetProperty( name)。SetValue(tp, Joh);
TP.GetProperty( link)。SetValue(tp, http://test.com);

// B类的设定值??????
// 在图表中添加tp

解决方案

要构造具有动态类型参数的泛型类型,您需要从泛型类型定义开始,并调用 MakeGenericType [ ^ ]:

输入myclassType = myclass.GetType(); 
类型listOfMyClass = typeof (List<>)。MakeGenericType(myclassType);
var myclass2 = MCB1.CreateObject( new string [] { graph}, new Type [] {listOfMyClass});



要创建列表实例并向其添加对象,请使用非泛型 IList 界面:

  //  创建新列表< A>: 
var graphs =(IList)Activator.CreateInstance(listOfMyClass);

// 创建一个新的B实例,并设置属性:
输入myclass2Type = myclass2.GetType();
object b = Activator.CreateInstance(myclass2Type);
myclass2Type.GetProperty( graph)。SetValue(b,graphs);

// 创建A的新实例并初始化其属性:
object a = Activator.CreateInstance(myclassType);
myclassType.GetProperty( id)。SetValue(a, 0 );
myclassType.GetProperty( name)。SetValue(a, Joh);
myclassType.GetProperty( link)。SetValue(a, http://test.com);

// 将项目添加到列表中:
graph 。新增的(a);


Hi guys

i have a xml file with this structure

<graph>
    <id>0</id>
    <name>John</name>
    <link>http://test.com</link>
</graph>
<graph>
    <id>1</id>
    <name>Roger</name>
    <link>http://test2.com</link>
</graph>



i want use Reflection.Emit class for create two class
first:

class A {
     int id;
     string name;
     string link
}


second:

Class B{
      List<A> graphs;
}




i read this paper (Introduction to Creating Dynamic Types with Reflection.Emit[^])and can create class A in runtime but the problem is using this (A) in another runtime class(B) and also have a List of A.


MyClassBuilder MCB=new MyClassBuilder("A");
var myclass = MCB.CreateObject(new string[3] { "id", "name", "link" }, new Type[3] { typeof(int), typeof(string), typeof(string) });
Type TP = myclass.GetType();
MyClassBuilder MCB1=new MyClassBuilder("B");
//Here is my confusion typeof(List<TP>) ?????? Error Ocurred
var myclass2 = MCB1.CreateObject(new string[1] {"graphs"}, new Type[1] {typeof(List<TP>)});
//set value of class A and work
object tp = Activator.CreateInstance(TP);
TP.GetProperty("id").SetValue(tp,0);
TP.GetProperty("name").SetValue(tp,"Joh");
TP.GetProperty("link").SetValue(tp,"http://test.com");

//set value of class B ??????
//add tp in graphs

解决方案

To construct a generic type with dynamic type parameters, you need to start with the generic type definition, and call MakeGenericType[^]:

Type myclassType = myclass.GetType();
Type listOfMyClass = typeof(List<>).MakeGenericType(myclassType);
var myclass2 = MCB1.CreateObject(new string[] { "graphs" }, new Type[] { listOfMyClass }); 


To create an instance of the list and add objects to it, use the non-generic IList interface:

// Create a new List<A>:
var graphs = (IList)Activator.CreateInstance(listOfMyClass);

// Create a new instance of "B", and set the property:
Type myclass2Type = myclass2.GetType();
object b = Activator.CreateInstance(myclass2Type);
myclass2Type.GetProperty("graphs").SetValue(b, graphs);

// Create a new instance of "A" and intialize its properties:
object a = Activator.CreateInstance(myclassType);
myclassType.GetProperty("id").SetValue(a, 0);
myclassType.GetProperty("name").SetValue(a, "Joh");
myclassType.GetProperty("link").SetValue(a, "http://test.com");

// Add the item to the list:
graphs.Add(a);


这篇关于如何在运行时创建两个嵌套类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆