Generic list of generic objects

后端 未结 3 1339
醉梦人生
醉梦人生 2020-12-01 12:06

Let\'s say I have an object that represents a field of data, that object needs the following properties: Name, Type, Value, Length. Here is the object:

class         


        
3条回答
  •  不思量自难忘°
    2020-12-01 12:53

    I suggest you to define an interface and Field implements that interface

    public interface IField
    {
    
    }
    
    public class Field : IField
    {
        public string Name { get; set; }
        public Type Type
        {
            get
            {
                return typeof(T);
            }
        }
        public int Length { get; set; }
        public T Value { get; set; }
    }
    

    so you can write this code:

    var list = new List();
    

    now this list can contain any object of type Field

提交回复
热议问题