Click here to Skip to main content
15,912,493 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys

i have a xml file with this structure
XML
<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:
C#
class A {
     int id;
     string name;
     string link
}

second:
C#
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.


C#
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
Posted
Updated 15-May-15 1:02am
v3
Comments
Sascha Lefèvre 15-May-15 6:35am    
Have you tried it?
eagers 15-May-15 6:51am    
i only can create the class A, after that i dont know how can graphs.SetValue of the secound class and how generate a type of a not existence Type.

in the paper all Type are int, string and all of them is exist! but in my problem want create Class B that have a property of Type A, is not exist.
eagers 15-May-15 6:59am    
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>) ??????
var myclass2 = MCB1.CreateObject(new string[1] {"graphs"}, new Type[1] {typeof(List<>)});
//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

1 solution

To construct a generic type with dynamic type parameters, you need to start with the generic type definition, and call MakeGenericType[^]:
C#
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:
C#
// 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);
 
Share this answer
 
v2
Comments
eagers 15-May-15 9:37am    
thanks for the good detail and answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900