在运行时向属性添加属性

时间:2014-05-13 09:33:49

标签: c# dynamic runtime

我有很多课

例如

  public class AccountItem
{
    public Guid p_ID { get; set; }
    public Guid LoginID { get; set; }
    public Guid DId { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string OldPassword { get; set; }
    public DateTime PassChangeDate { get; set; }
}

    public class RankItem
{
    public Guid ModID { get; set; }
    public string Code { get; set; }
    public string RankName { get; set; }
    public int? Priority { get; set; }
}

以及更多......

如何添加记录类添加动态字段或类?

例如

我使用这个结构

 public class RecordData
{
    public Guid className { get; set; } 
    public DataFields dataFields { get; set; } 
}



   public class DataFields
{
    public List<AccountItem> Items { get; set; }

    public DataFields()
    {
        Items = new List<AccountItem>();
    }  
}   

 public class DataFields
{
    public List<RankItem> Items { get; set; }

    public DataFields()
    {
        Items = new List<RankItem>();
    }  
}  

如何添加记录类添加动态运行时字段或类?

public class DataFields
{
    public List<????> Items { get; set; }

    public DataFields()
    {
        Items = new List<????>();  //I want to add dynamic list field
    }  
}  


public class RankItem
{
    public Guid ModID { get; set; }
    public string Code { get; set; }
    public string RankName { get; set; }
    public int? Priority { get; set; }

    publist string or type  ????  {get;set;}  // I want to add dynamic field
}

谢谢

2 个答案:

答案 0 :(得分:0)

符合某些内容(可能有一些语法错误,但这是个主意)

public class DataFields<T>
{
    public List<T> Items { get; set; }

    public DataFields()
    {
        Items = new List<T>(); 
    }  
}  

并使用它:

public class RankItem
{
    public Guid ModID { get; set; }
    public string Code { get; set; }
    public string RankName { get; set; }
    public int? Priority { get; set; }

    public DataFields<string> StringDataFields {get;set;}  // I want to add dynamic field
    public  DataFields<AccountItem> AccountItemDataFields {get;set;}  // I want to add dynamic field
    //public DataFields<int> AnotherSampleDataList {get;set;}  // I want to add dynamic field
}

答案 1 :(得分:0)

    public class RecordData
{
    public Guid className { get; set; }
    public DataFields<AccountItem> dataFields { get; set; }  //or  public DataFields<RankItem> dataFields { get; set; }
}



public class DataFields<T>
{
    public List<T> Items { get; set; }

    public DataFields()
    {
        Items = new List<T>();
    }
}