2012-02-09 50 views
0

这里是我的代码:如何创建在C#(Silverlight的)动态类在那里我可以遍历性

ObservableCollection<object> ll = new ObservableCollection<object>(); 

public MainPage(){ 

InitializeComponent(); 

ll= createobj(x2); 

     dataGrid2.ItemsSource = ll; 

这是造成我的财产的功能。 我该如何让他们公开?

private PropertyInfo papa(string propertyName, TypeBuilder tb, Type tt){ 


private PropertyInfo papa(string propertyName, TypeBuilder tb, Type tt){ 
FieldBuilder ff = tb.DefineField("_" + propertyName, tt, FieldAttributes.Public); 
PropertyBuilder pp = 
      tb.DefineProperty(propertyName, 
          PropertyAttributes.None , 
          tt, 
          new Type[] {tt }); 


     MethodBuilder mget = 
      tb.DefineMethod("get_value", 
             MethodAttributes.Public, 
             tt, 
             Type.EmptyTypes); 

     ILGenerator currGetIL = mget.GetILGenerator(); 
     currGetIL.Emit(OpCodes.Ldarg_0); 
     currGetIL.Emit(OpCodes.Ldfld, ff); 
     currGetIL.Emit(OpCodes.Ret); 


     MethodBuilder mset = 
      tb.DefineMethod("set_value", 
             MethodAttributes.Public, 
             null, 
             new Type[] { tt }); 


     ILGenerator currSetIL = mset.GetILGenerator(); 
     currSetIL.Emit(OpCodes.Ldarg_0); 
     currSetIL.Emit(OpCodes.Ldarg_1); 
     currSetIL.Emit(OpCodes.Stfld, ff); 
     currSetIL.Emit(OpCodes.Ret); 


     pp.SetGetMethod(mget); 
     pp.SetSetMethod(mset); 
     return pp; 
    } 

这是我的创建对象的函数

private ObservableCollection<object> createobj(XDocument xx){ 

     AssemblyName assemblyName = new AssemblyName(); 
     assemblyName.Name = "tmpAssembly"; 
     AssemblyBuilder assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); 
     ModuleBuilder module = assemblyBuilder.DefineDynamicModule("tmpModule"); 


     TypeBuilder tb = module.DefineType("SilverlightApplication20.blabla", TypeAttributes.Public | TypeAttributes.Class); 
     int[] exista={0,0}; 
     PropertyInfo pp; 

     foreach (XElement node in xx.Root.Descendants()) 
     { 
      foreach (XAttribute xa in node.Attributes()) 
      { 
       if (xa.Name.ToString() != "rind" && xa.Name.ToString() != "col") 
        pp = papa(xa.Name.ToString(), tb, typeof(string)); 
       else 
        pp = papa(xa.Name.ToString(), tb, typeof(int)); 

      } 
     } 

     pp=papa("nume",tb,typeof(string)); 
     pp = papa("parinte", tb, typeof(string)); 
     Type gg = tb.CreateType(); 

     ObservableCollection<object> collection = new ObservableCollection<object>(); 

     PropertyInfo[] pps = gg.GetProperties(); 

     foreach (XElement node in xx.Root.Descendants()) 
     { 
      object obiect = Activator.CreateInstance(gg); 
      foreach (PropertyInfo property in pps) 
      { if (property.Name == "nume") 
        property.SetValue(obiect, node.Name.ToString(),null); 
      if (property.Name == "parinte") 
       property.SetValue(obiect, node.Parent.Name.ToString(), null); 
      } 
      foreach (XAttribute xa in node.Attributes()) 
      { 
        string value=""; 
       int value2=0; 
       { if(xa.Name.ToString()!="rind" && xa.Name.ToString()!="col") 
        value = xa.Value; 
       else 
        value2 = int.Parse(xa.Value); 

        foreach (PropertyInfo property in pps) 
        { 
         if (property.Name == xa.Name.ToString()) 
         { 
          if(xa.Name.ToString()=="rind" || xa.Name.ToString()=="col") 
           property.SetValue(obiect, value2, null); 
          else 
          property.SetValue(obiect, value, null); 
          break; 
         } 
        } 
       } 

      } collection.Add(obiect); 
     } 
     return collection; 

    } 

的问题是,我可以不通过性循环。

我想是这样创造的东西:

public class blabla 
    { 
    public int property1{get;set;} 
    public int property2{get;set;} 

    } 

,并能够做这样的事

object1.property=1; 

这就是我需要: 我有一个XML字符串,它看起来像这个:

     <xml> 
        <col1 label="label1" r="1" c="1"/> 
        <col2 label="label2" r="2" c="1"/> 
        <col3 label="label2" r="2" c="2"/> 

              < /xml> 

我想绑定到一个数据网格。 问题是我不知道在运行时会有多少属性。

对于上面的例子我可以创建一个类是这样的:

public class blabla 
    { 
    public string labe{get;set;} 
    public int r{get;set;} 
    public int c{get;set;} 
    } 

但正如我所说可以有更多的属性。这就是为什么我需要动态的东西。 在同一时间我需要能够遍历创建的属性

+0

为什么使用'objects'而不是'blabla'类?你想做什么?? – ken2k 2012-02-09 12:49:47

+0

一个动态类。我有一个字符串xml,我想创建一个具有这些属性的动态类(xml属性) – mertin 2012-02-09 12:53:20

+0

这看起来像一个**糟糕的**方法。你应该编辑你的答案,给我们更多关于你的需求的背景,我相信我们能够给你一个更合理的方式去。 – ken2k 2012-02-09 12:56:33

回答

0

我会说你最好的选择是将源XML转换为DataSet或IEnumerable,然后将其绑定到你的网格。

相关问题