program tip

새로운 익명 클래스를 동적으로 만드는 방법은 무엇입니까?

radiobox 2020. 9. 4. 07:08
반응형

새로운 익명 클래스를 동적으로 만드는 방법은 무엇입니까?


C # 3.0에서는 다음 구문을 사용하여 익명 클래스를 만들 수 있습니다.

var o1 = new { Id = 1, Name = "Foo" };

이러한 익명 클래스를 변수에 동적으로 만드는 방법이 있습니까?


예:

var o1 = new { Id = 1, Name = "Foo" };
var o2 = new { SQ = 2, Birth = DateTime.Now };

동적 생성 예 :

var o1 = DynamicNewAnonymous(new NameValuePair("Id", 1), new NameValuePair("Name", "Foo"));
var o2 = DynamicNewAnonymous(new NameValuePair("SQ", 2), new NameValuePair("Birth", 
DateTime.Now));

해야 할 일이 있기 때문에 :

dynamic o1 = new ExpandObject(); 
o1."ID" = 1;    <--"ID" is dynamic name
o1."Name" = "Foo";  <--"Name" is dynamic name

그리고 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));
  }
}

내가 전화하면 :

dynamic o1 = new ExpandObject();
o1.Name = "123";
ShowPropertiesValue(o1);

결과를 표시 할 수 없습니다.

Name = 123

또한 ExpandoObject를 AnonymouseType으로 변환하는 방법은 무엇입니까?

Type type = o1.GetType();
type.GetProperties();   <--I hope it can get all property of o1

마지막으로 ShowPropertiesValue () 메서드를 수정합니다.

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));
    } 
  }
}

DynamicNewAnonymous 메서드를 구현하는 방법 또는 ShowPropertiesValue ()를 수정하는 방법은 무엇입니까?

내 동기는 :

dynamic o1 = new MyDynamic();
o1.Name = "abc";
Type o1Type = o1.GetType();
var props = o1Type.GetProperties(); <--I hope can get the Name Property

dynamicObject의 GetType 메소드를 연결할 수 있고 Compel은 강력한 유형의 유형으로 변환합니다. 위의 Seamless 코드는 잘 작동 할 수 있습니다.


익명 형식은 암시 적으로 선언 된 일반 형식입니다. 그들은 dynamic.

Now, if you were to use an ExpandoObject and reference it through a dynamic variable, you could add or remove fields on the fly.

edit

Sure you can: just cast it to IDictionary<string, object>. Then you can use the indexer.

You use the same casting technique to iterate over the fields:

dynamic employee = new ExpandoObject();
employee.Name = "John Smith";
employee.Age = 33;

foreach (var property in (IDictionary<string, object>)employee)
{
    Console.WriteLine(property.Key + ": " + property.Value);
}
// This code example produces the following output:
// Name: John Smith
// Age: 33

The above code and more can be found by clicking on that link.


You can create an ExpandoObject like this:

IDictionary<string,object> expando = new ExpandoObject();
expando["Name"] = value;

And after casting it to dynamic, those values will look like properties:

dynamic d = expando;
Console.WriteLine(d.Name);

However, they are not actual properties and cannot be accessed using Reflection. So the following statement will return a null:

d.GetType().GetProperty("Name") 

참고URL : https://stackoverflow.com/questions/3740021/how-to-dynamic-new-anonymous-class

반응형