Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有或约束的泛型类型属性_C#_.net_Generics - Fatal编程技术网

C# 具有或约束的泛型类型属性

C# 具有或约束的泛型类型属性,c#,.net,generics,C#,.net,Generics,是否可能有一个独立于类型的属性类,可能用“where或”约束修饰 例如: public class Attribute<T> where T: string || bool { public const char ATTRIBUTE_CHAR = 'a'; public string Key { get; set; } public T Value { get; set; } public Attribute(string key, T

是否可能有一个独立于类型的属性类,可能用“where或”约束修饰

例如:

public class Attribute<T> where T: string || bool {

   public const char ATTRIBUTE_CHAR = 'a';

   public string Key    { get; set; }
   public T      Value  { get; set; }

   public Attribute(string key, T value) {
       Key   = key;
       Value = value;
   }

   public Attribute(string key, bool value = true)
   : base(key, value) {}

   public Attribute(string key, string value)
   : base(key, value) {}

   public override string ToString() {

        if(typeof(value) == bool && (bool)value)
             return String.Format({0}={1},
                 ATTRIBUTE_CHAR, key);
        else return String.Format({0}={1}:{2},
            ATTRIBUTE_CHAR, key, (string)value);
   }

}
但这样的事情永远不可能发生:

Attribute<int>...
属性。。。

没有
。您可能想做的是:

interface IKeyValue<TKey, TValue>
{
   public TKey Key {get;set;}
   public TValue Value {get;set;}
}

public class Attribute : IKeyValue<string, string>
{
   public override string ToString() 
   {
        return String.Format("{0}={1}:{2}", Constants.ATTRIBUTE_CHAR, Key, Value);
   }
}

public class BoolAttribute : IKeyValue<string, bool>
{
   public override string ToString() 
   {
        return Value ? String.Format("{0}={1}", Constants.ATTRIBUTE_CHAR, Key) : String.Empty;
   }
}
接口IKeyValue
{
公钥{get;set;}
公共TValue值{get;set;}
}
公共类属性:IKeyValue
{
公共重写字符串ToString()
{
返回String.Format(“{0}={1}:{2}”,Constants.ATTRIBUTE_CHAR,Key,Value);
}
}
公共类boolatAttribute:IKeyValue
{
公共重写字符串ToString()
{
返回值?String.Format(“{0}={1}”,Constants.ATTRIBUTE_CHAR,Key):String.Empty;
}
}

没有
。您可能想做的是:

interface IKeyValue<TKey, TValue>
{
   public TKey Key {get;set;}
   public TValue Value {get;set;}
}

public class Attribute : IKeyValue<string, string>
{
   public override string ToString() 
   {
        return String.Format("{0}={1}:{2}", Constants.ATTRIBUTE_CHAR, Key, Value);
   }
}

public class BoolAttribute : IKeyValue<string, bool>
{
   public override string ToString() 
   {
        return Value ? String.Format("{0}={1}", Constants.ATTRIBUTE_CHAR, Key) : String.Empty;
   }
}
接口IKeyValue
{
公钥{get;set;}
公共TValue值{get;set;}
}
公共类属性:IKeyValue
{
公共重写字符串ToString()
{
返回String.Format(“{0}={1}:{2}”,Constants.ATTRIBUTE_CHAR,Key,Value);
}
}
公共类boolatAttribute:IKeyValue
{
公共重写字符串ToString()
{
返回值?String.Format(“{0}={1}”,Constants.ATTRIBUTE_CHAR,Key):String.Empty;
}
}

您可以创建抽象属性和所需的实现。 然后可以按要求的方式处理每个属性。(强制转换为属性),但无法创建不受支持类型的属性

public abstract class Attribute<T>
{
    public abstract T getValue();
}

public class StringAttribute : Attribute<String>
{
    String value;

    public String getValue(){
        return value;
    }

}

public class BooleanAttribute : Attribute<Boolean>
{
    Boolean value;

    public Boolean getValue()
    {
        return value;
    }
}
公共抽象类属性
{
公共抽象T getValue();
}
公共类StringAttribute:属性
{
字符串值;
公共字符串getValue(){
返回值;
}
}
公共类BooleanAttribute:属性
{
布尔值;
公共布尔getValue()
{
返回值;
}
}

这也允许您非常容易地实现依赖于类型的属性函数。(如toString()等)

您可以创建抽象属性和所需的实现。 然后可以按要求的方式处理每个属性。(强制转换为属性),但无法创建不受支持类型的属性

public abstract class Attribute<T>
{
    public abstract T getValue();
}

public class StringAttribute : Attribute<String>
{
    String value;

    public String getValue(){
        return value;
    }

}

public class BooleanAttribute : Attribute<Boolean>
{
    Boolean value;

    public Boolean getValue()
    {
        return value;
    }
}
公共抽象类属性
{
公共抽象T getValue();
}
公共类StringAttribute:属性
{
字符串值;
公共字符串getValue(){
返回值;
}
}
公共类BooleanAttribute:属性
{
布尔值;
公共布尔getValue()
{
返回值;
}
}

这也允许您非常容易地实现依赖于类型的属性函数。(如toString()等)

您能澄清属性类的用途吗?@daryal在库中解析并提供类似的内容:
a=recvonly CRLF a=rtpmap:99 h263-1998/90000
这里有两种类型的属性,一种是布尔属性(只有“启用”属性的键,如果为false,则不会出现在消息中),一个是字符串(键“rtpmap”和值“99 h263…”)为什么需要泛型?在
属性
类中没有任何通用逻辑。泛型的要点是以相同的方式处理不同类型的对象。如果需要像
If(typeof(T)==bool)这样的行
那么这是一个信号,表明你做错了。要回答你的问题-不,没有
@NikitaBrizhak Uff,谢谢你澄清我的问题。你介意把你的评论作为答案发表吗?这样我就可以接受了?你能澄清属性类的用途吗?daryal解析并提供一些信息在库中类似这样:
a=recvonly CRLF a=rtpmap:99 h263-1998/90000
这里有两种类型的属性,一种是布尔属性(仅一个“启用”属性的键,如果为false,则不会出现在消息中),另一种是字符串(一个键“rtpmap”和一个值“99 h263…)为什么需要泛型?在
属性
类中没有任何通用逻辑。泛型的要点是以相同的方式处理不同类型的对象。如果需要像
If(typeof(T)==bool)这样的行
那么这是一个信号,表明你做错了。回答你的问题-不,没有
@NikitaBrizhak Uff,谢谢你澄清我的问题。请你将你的评论作为答案发表,这样我就可以接受了吗?可能是重复的