3,0 dinamik bir değişkene bu anonim sınıf oluşturmak için bir yol var mı şu sözdizimiYeni Anonim Sınıf nasıl dinamik yapılır? C# '
var o1 = new { Id = 1, Name = "Foo" };
ile anonim sınıf oluşturabilir?
Örnek: yapmam gereken
var o1 = DynamicNewAnonymous(new NameValuePair("Id", 1), new NameValuePair("Name", "Foo"));
var o2 = DynamicNewAnonymous(new NameValuePair("SQ", 2), new NameValuePair("Birth",
DateTime.Now));
BEACUSE:
var o1 = new { Id = 1, Name = "Foo" };
var o2 = new { SQ = 2, Birth = DateTime.Now };
Dinamik Örnek oluşturmak
dynamic o1 = new ExpandObject();
o1."ID" = 1; <--"ID" is dynamic name
o1."Name" = "Foo"; <--"Name" is dynamic name
Ve Scene1:
void ShowPropertiesValue(object o)
{
Type oType = o.GetType();
foreach(var pi in oType.GetProperties())
{
Console.WriteLine("{0}={1}", pi.Name, pi.GetValue(o, null));
}
}
Ararsam:
dynamic o1 = new ExpandObject();
o1.Name = "123";
ShowPropertiesValue(o1);
Bu sonuç gösteremez:
Name = 123
Ve ayrıca nasıl AnonymouseType için ExpandoObject değiştirmek için?
Type type = o1.GetType();
type.GetProperties(); <--I hope it can get all property of o1
Son, ben) (
void ShowPropertiesValue(object o)
{
if(o is static object) <--How to check it is dynamic or static object?
{
Type oType = o.GetType();
foreach(var pi in oType.GetProperties())
{
Console.WriteLine("{0}={1}", pi.Name, pi.GetValue(o, null));
}
}
else if(o is dynamic object) <--How to check it is dynamic or static object?
{
foreach(var pi in ???) <--How to get common dynamic object's properties info ?
{
Console.WriteLine("{0}={1}", pi.Name, pi.GetValue(o, null));
}
}
}
Nasıl DynamicNewAnonymous yöntemini uygulamak için ShowPropertiesValue() yöntemi değiştirmek veya nasıl ShowPropertiesValue değiştirmek için?
Benim motivasyonları geçerli:
dynamic o1 = new MyDynamic();
o1.Name = "abc";
Type o1Type = o1.GetType();
var props = o1Type.GetProperties(); <--I hope can get the Name Property
i dynamicObject en GetType Yöntemi kanca ve Compel Tipi kesinlikle yazılan dönüştürmek edin. Yukarıdaki Dikişsiz kod düzgün çalışabilir.
@Vlad: Ben motivasyonları hakkında biraz belirsiz değilim itiraf. –
@VladLazarenko Bence haklısın :-) – oberfreak
Lütfen bize ne yapmak istediğini ve bunun neden senin seçilmiş çözüm olduğunu söyle. – oberfreak