Dynamic class creation in C#

前端 未结 5 766
离开以前
离开以前 2021-01-13 10:07

Is it possible in runtime to create a class from DataTable where ColumnName will be dynamic-class properties?

5条回答
  •  长发绾君心
    2021-01-13 10:51

    With C# 4, you can do this

    dynamic foo = new ExpandoObject();
    
    // mimic grabbing a column name at runtime and adding it as a property
    ((IDictionary)foo).Add("Name", "Apple");
    
    Console.WriteLine(foo.Name); // writes Apple to screen
    

    Not recommending it or anything, but it shows you it is possible.

提交回复
热议问题