Dynamic class creation in C#

前端 未结 5 747
离开以前
离开以前 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:41

    Reading your comments, I undestood your mean. Just use Generics: using List fields to generate the objects. The code is quite simple:

    public class DynClass
        {
            public DynClass()
            {
                _fields = new Dictionary();
            }
    
            private IDictionary _fields;
    
            public IDictionary Fields
            {
                get { return _fields; }
            }
    
        }
    
        public class TestGenericInstances
        {
            public TestGenericInstances()
            {
                Client cli = new Client("Ash", "99999999901");
    
                /* Here you can create any instances of the Class. 
                 * Also DynClass
                 * */
                DynClass gen = new DynClass();
    
                /* Add the fields
                 * */
                gen.Fields.Add("clientName", cli);
    
                /* Add the objects to the List
                 * */
                List lstDyn = new List().Add(gen);
            }        
        }
    
        

    提交回复
    热议问题