如何在c#(silverlight)中创建一个动态类,在其中可以循环访问属性
这是我的密码:如何在c#(silverlight)中创建一个动态类,在其中可以循环访问属性,c#,silverlight,reflection,loops,properties,C#,Silverlight,Reflection,Loops,Properties,这是我的密码: ObservableCollection<object> ll = new ObservableCollection<object>(); public MainPage(){ InitializeComponent(); ll= createobj(x2); dataGrid2.ItemsSource = ll; 这是创建我的对象的函数 private ObservableCollection<object>
ObservableCollection<object> ll = new ObservableCollection<object>();
public MainPage(){
InitializeComponent();
ll= createobj(x2);
dataGrid2.ItemsSource = ll;
这是创建我的对象的函数
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;
}
并且能够做这样的事情
object1.property=1;
这就是我需要的:
我有一个xml字符串,如下所示:
public class blabla
{
public int property1{get;set;}
public int property2{get;set;}
}
<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;}
}
但正如我所说,还有更多的属性。这就是为什么我需要一些动态的东西。
同时,我需要能够迭代创建的属性,我认为最好的方法是将源XML转换为数据集或IEnumerable,然后将其绑定到网格。据我所知,您正试图将一组对象绑定到一个数据网格,而您不知道该对象在编译时的样子 这篇博文讨论了这个问题:
如果我误解了你的问题,请告诉我。为什么要使用
对象
而不是blabla
类?你想做什么?一个动态类。我有一个字符串xml,我想用这些属性(xml中的属性)创建一个动态类。这看起来确实是一个糟糕的方法。您应该编辑您的答案,并向我们提供更多有关您需求的背景信息,我相信我们能够为您提供更合理的解决方案。您希望从中获得什么,而不是使用字典或expando(动态)对象?我不能使用字典,因为我无法将其绑定到数据网格
public class blabla
{
public string labe{get;set;}
public int r{get;set;}
public int c{get;set;}
}