Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.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,我正在尝试创建一个公共属性,它可以是long类型,也可以是Guid类型。泛型有可能吗?e、 差不多 public virtual T where T: long or Gui Id { get; set; } 不,这是不可能的。如果只有两种可能的类型,只需编写两次类,在一个公共基类中放入尽可能多的公共代码?不,这是不可能的。如果只有两种可能的类型,只需编写两次类,在一个公共基类中放入尽可能多的公共代码?不,这是不可能的。没有这样的限制。这里有一个问题。不,那是不可能的。没有这样的限制。这是一个

我正在尝试创建一个公共属性,它可以是long类型,也可以是Guid类型。泛型有可能吗?e、 差不多

public virtual T where T: long or Gui Id { get; set; }

不,这是不可能的。如果只有两种可能的类型,只需编写两次类,在一个公共基类中放入尽可能多的公共代码?

不,这是不可能的。如果只有两种可能的类型,只需编写两次类,在一个公共基类中放入尽可能多的公共代码?

不,这是不可能的。没有这样的限制。这里有一个问题。

不,那是不可能的。没有这样的限制。这是一个。

不,这是不可能的,您必须选择一个相同的父类,或者您应该编写一个可以同时存储这两个类的类的实现

比如:

class LongOrGuid
{
     private Guid _guid;
     private long _long;
     private bool _isGuid = true;

     public LongOrGuid(Guid g)
     {
          _guid = g;
          _isGuid = true;
     }

     public LongOrGuid(long l)
     {
          _long = l;
          _isGuid = false;
     }

     public long Long
     {
          get
          {
               if(_isGuid)
               {
                    throw new NotSupportedException("This is a guid");
               }
               return _long;
          }
     }

     public Guid Guid
     {
          get
          {
               if(!_isGuid)
               {
                    throw new NotSupportedException("This is a long");
               }
               return _guid;
          }
     }

     public bool IsGuid
     {
          get
          {
               return _isGuid;
          }
     }
}

不,这是不可能的,您必须选择一个相同的父类,或者您应该编写自己的类实现来存储这两个类

比如:

class LongOrGuid
{
     private Guid _guid;
     private long _long;
     private bool _isGuid = true;

     public LongOrGuid(Guid g)
     {
          _guid = g;
          _isGuid = true;
     }

     public LongOrGuid(long l)
     {
          _long = l;
          _isGuid = false;
     }

     public long Long
     {
          get
          {
               if(_isGuid)
               {
                    throw new NotSupportedException("This is a guid");
               }
               return _long;
          }
     }

     public Guid Guid
     {
          get
          {
               if(!_isGuid)
               {
                    throw new NotSupportedException("This is a long");
               }
               return _guid;
          }
     }

     public bool IsGuid
     {
          get
          {
               return _isGuid;
          }
     }
}
泛型有可能吗

这是不可能的,但您可以通过使用来解决此问题,以支持long和Guid,示例代码:

internal class YourId
{
    public long LongId { get; private set; }
     public Guid GuidId { get; private set; }

    public YourId(long longValue)
    {
        LongId = longValue;
    }

    public YourId(Guid guidValue)
    {
        GuidId = guidValue;
    }

    public static implicit operator long(YourId yourId)
    {
        return yourId.LongId;
    }

    public static  implicit operator YourId(long value)
    {
        return new YourId(value);
    }

       public static implicit operator Guid(YourId yourId)
    {
        return yourId.GuidId;
    }

    public static  implicit operator YourId(Guid value)
    {
        return new YourId(value);
    }
}
现在您可以使用:

YourId id1 = Guid.NewGuid();
YourId id2 = long.MaxValue;
泛型有可能吗

这是不可能的,但您可以通过使用来解决此问题,以支持long和Guid,示例代码:

internal class YourId
{
    public long LongId { get; private set; }
     public Guid GuidId { get; private set; }

    public YourId(long longValue)
    {
        LongId = longValue;
    }

    public YourId(Guid guidValue)
    {
        GuidId = guidValue;
    }

    public static implicit operator long(YourId yourId)
    {
        return yourId.LongId;
    }

    public static  implicit operator YourId(long value)
    {
        return new YourId(value);
    }

       public static implicit operator Guid(YourId yourId)
    {
        return yourId.GuidId;
    }

    public static  implicit operator YourId(Guid value)
    {
        return new YourId(value);
    }
}
现在您可以使用:

YourId id1 = Guid.NewGuid();
YourId id2 = long.MaxValue;

属性不能是那样的泛型。也许可以将包含属性的类改为泛型

不能限制为long或Guid。你可以说:

class ClassContainingYourProperty<T> where T : struct, IFormattable, IComparable<T>, IEquatable<T>
{
  static ClassContainingYourProperty() // do type check in static constructor
  {
    var t = typeof(T);
    if (t != typeof(long) && t != typeof(Guid))
      throw new NotSupportedException("T cannot be " + t);
  }

  public virtual T YourProperty { get; set; }
}

属性不能是那样的泛型。也许可以将包含属性的类改为泛型

不能限制为long或Guid。你可以说:

class ClassContainingYourProperty<T> where T : struct, IFormattable, IComparable<T>, IEquatable<T>
{
  static ClassContainingYourProperty() // do type check in static constructor
  {
    var t = typeof(T);
    if (t != typeof(long) && t != typeof(Guid))
      throw new NotSupportedException("T cannot be " + t);
  }

  public virtual T YourProperty { get; set; }
}

那是不可能的。你想达到什么目标?也许我们可以找到一个解决方案。你可以,但是你必须在c中创建一个联合类型,因为它不是现成的。看,那是不可能的。你想达到什么目标?也许我们可以找到一个解决方案。你可以,但是你必须在c中创建一个联合类型,因为它不是现成的。看见