ループスルーはここに私のコードです:どこでできるプロパティ
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;}
}
を私が言ったように、多くのより多くの属性があることができます:
上記の例のために私はこのようなクラスを作成することができます。そのため私は何かを動的に必要としています。 同時に、作成されたプロパティを繰り返し処理できるようにする必要があります
なぜあなたの 'blabla'クラスの代わりに' objects'を使うのですか?あなたは何をしようとしているのですか?? – ken2k
は動的クラスです。私は文字列xmlを持っており、これらのプロパティ(xmlの属性)を使って動的クラスを作成したい。 – mertin
これは本当に**悪い**アプローチのように見える。あなたの答えを編集して、あなたのニーズについてもっと多くの背景を教えてください、私たちはあなたにもっと合理的な道を与えることができると確信しています。 – ken2k