C# 是否可以基于另一个属性指定并设置类属性的默认值?

C# 是否可以基于另一个属性指定并设置类属性的默认值?,c#,asp.net,.net,class,generics,C#,Asp.net,.net,Class,Generics,我有一个列表,它有值属性和CharToRead 我正在试图找到一种方法,将CharToRead自动设置为值。长度 我认为这可以通过构造函数实现,但我甚至不能创建构造函数,因为我有一个泛型类 类别: public class HeaderType<T> { public string FieldTag { get; set; } public string FieldName { get; set; } public stri

我有一个列表,它有
属性和
CharToRead
我正在试图找到一种方法,将
CharToRead
自动设置为
值。长度

我认为这可以通过构造函数实现,但我甚至不能创建构造函数,因为我有一个泛型类

类别:

  public class HeaderType<T>
    {
        public string FieldTag { get; set; }
        public string FieldName { get; set; }
        public string Value { get; set; }
    }

public class HeaderTypeEnq<T> : HeaderType<T>
{
    public string Mandatory { get; set; }
    public string CharacterType { get; set; }
    public string FixedLength { get; set; }
    public int Position { get; set; }
    public int MaxLength { get; set; }
    public int CharToRead { get; set; }    
}
  List<HeaderTypeEnq<dynamic>> PNListEnq = new List<HeaderTypeEnq<dynamic>>();
            PNListEnq.Add(new HeaderTypeEnq<dynamic>() { FieldTag = "PN", FieldName = "Segment Tag", Value = "PN", Mandatory = "Y", CharacterType = "A/N", Position = 0, MaxLength = 04, FixedLength = "Y", CharToRead= ?  }); // replace ? with length of Value
公共类标题类型
{
公共字符串字段标记{get;set;}
公共字符串字段名{get;set;}
公共字符串值{get;set;}
}
公共类HeaderType问:HeaderType
{
必须使用公共字符串{get;set;}
公共字符串字符类型{get;set;}
公共字符串固定长度{get;set;}
公共int位置{get;set;}
公共int MaxLength{get;set;}
public int CharToRead{get;set;}
}
列表:

  public class HeaderType<T>
    {
        public string FieldTag { get; set; }
        public string FieldName { get; set; }
        public string Value { get; set; }
    }

public class HeaderTypeEnq<T> : HeaderType<T>
{
    public string Mandatory { get; set; }
    public string CharacterType { get; set; }
    public string FixedLength { get; set; }
    public int Position { get; set; }
    public int MaxLength { get; set; }
    public int CharToRead { get; set; }    
}
  List<HeaderTypeEnq<dynamic>> PNListEnq = new List<HeaderTypeEnq<dynamic>>();
            PNListEnq.Add(new HeaderTypeEnq<dynamic>() { FieldTag = "PN", FieldName = "Segment Tag", Value = "PN", Mandatory = "Y", CharacterType = "A/N", Position = 0, MaxLength = 04, FixedLength = "Y", CharToRead= ?  }); // replace ? with length of Value
List PNListEnq=new List();
PNListEnq.Add(新标题类型enq(){FieldTag=“PN”,FieldName=“Segment Tag”,Value=“PN”,Mandatory=“Y”,CharacterType=“A/N”,Position=0,MaxLength=04,FixedLength=“Y”,CharToRead=?});//代替有价值的长度
“我甚至不能创建构造函数,因为我有一个泛型 阶级。”

这就是为什么,您可以创建泛型类型的实例,并因此构建构造函数:

public HeaderTypeEnq<T>(string value) {
    this.Value = value;
    this.CharToRead = this.Value.Length;
}
public headerTypeNq(字符串值){
这个。值=值;
this.CharToRead=this.Value.Length;
}

然后在consumer类中,您可以只使用Value.Length。但如果您仍然想使用CharToRead,这将是解决方案:

public class HeaderTypeEnq<T> : HeaderType<T>
{
    public string Mandatory { get; set; }
    public string CharacterType { get; set; }
    public string FixedLength { get; set; }
    public int Position { get; set; }
    public int MaxLength { get; set; }
    public int CharToRead
    {
        get
        {
            if (string.IsNullOrEmpty(Value))
            {
                return 0;
            }
            return Value.Length;
        }
    }
}
公共类HeaderType enq:HeaderType
{
必须使用公共字符串{get;set;}
公共字符串字符类型{get;set;}
公共字符串固定长度{get;set;}
公共int位置{get;set;}
公共int MaxLength{get;set;}
沙图雷德公共酒店
{
得到
{
if(string.IsNullOrEmpty(Value))
{
返回0;
}
返回值。长度;
}
}
}

根据您告诉我们的内容,我不知道有什么好方法可以做到这一点,但您肯定可以在泛型类上使用构造函数,因此您可以这样做(当然,使用您想要的任何参数):


当然,这还需要修改基类(至少要使该属性
虚拟
),这可能是可行的,也可能是不可行的。

泛型类型可以有构造函数,您可以通过不使用自动属性语法来定制属性get/set逻辑。是否希望
public int CharToRead{get{return string.IsNullOrEmpty(Value)?0:base.Value.Length;}
?是的,但我必须传递一个参数。我不想传递一个参数。应该检查null值添加null检查,这将是完美的。
private string _value;
public string Value
{
    get
    {
        return this._value;
    }
    set
    {
        this._value = value;
        this.CharToRead = value.Length;
    }
}