简体   繁体   English

C#中变量类型的多态性

[英]Polymorphism on type of Variables in C#

I have a variable in a Class with the name of Id : 我在一个名为Id的类中有一个变量:

public class myClass
{
    GUID Id;
}

How can I have polymorphism on the Id variable to Get either an int type or a GUID type? 如何在Id变量上使用多态来获取int类型或GUID类型?

I want something like this, but it only gets GUID or int: 我想要这样的东西,但它只获得GUID或int:

EDIT: 编辑:

public class myClass
{
    Guid || int Id;
}

I want it so that the int or Guid is defined on initialization of the class. 我想要它,以便在类的初始化时定义int或Guid。

You could make the class generic: 你可以使这个类通用:

public class MyThing<T>
  where T : struct
{
  public T MyProperty { get; set; }
}

Which then can be used the following way: 然后可以使用以下方式:

MyThing<GUID>
MyThing<int>

But to give you a valid answer, it would certainly help to know what you are trying to achieve. 但是为了给你一个有效的答案,肯定会有助于你知道你想要达到的目标。

You can use a generic class. 您可以使用泛型类。

public class MyClassName<TId>
{
  public TId Id { get; set; }
}

And use it like this: 并像这样使用它:

For int: 对于int:

var myObject = new MyClassName<int>();

For Guid: 对于Guid:

var myObject = new MyClassName<Guid>();

I am not sure why you would want to do that, but if you must, how about: 我不确定你为什么要这样做,但如果你必须,那怎么样:

public class
{
    object Id;
}

or 要么

public class
{
   private int _intId;
   private GUID _guidId;

   public int GetIntegerGuid()
   { 
       return _intId;
   }

   public int GetGuid()
   { 
       return _guidId;
   }
}

You cannot do that in this case as polymorphism is something related to the object inheritance 在这种情况下你不能这样做,因为多态是与对象继承相关的东西

Polymorphism is the ability for an object to change its public interface according to how it is being used. 多态性是对象根据其使用方式更改其公共接口的能力。 When using inheritance, polymorphism is achieved when an object of a derived class is substituted where an instance of its parent class is expected. 使用继承时,如果派生类的对象被替换为期望其父类的实例,则实现多态。 This uses a process known as upcasting. 这使用称为向上转换的过程。 With upcasting, the object of the more specialised class is implicitly cast to the base class type required. 通过向上转换,更专业的类的对象被隐式转换为所需的基类类型。

more info here 更多信息在这里

What you want is a union in C or a discriminated union type in F# ; 你想要的是C中unionF#中区别的联合类型; neither is possible to be defined in a nice syntax in C# . 两者都不可能在C#中用一个很好的语法定义。

This code provides you a generic class for using in the like scenarios: 此代码为您提供了在类似方案中使用的通用类:

public class Either<A, B>
{
    A _a;
    B _b;
    bool _isLeft;

    private Either() { }
    private Either(A a, B b, bool isLeft)
    {
        _isLeft = isLeft;

        if (_isLeft)
        {
            _a = a;
            return;
        }

        _b = b;
    }
    public Either(A a) : this(a, default(B), true) { }
    public Either(B b) : this(default(A), b, false) { }

    public A Left { get { return _a; } }
    public B Right { get { return _b; } }
    public bool HasLeft { get { return _isLeft; } }
}

You need to check HasLeft to see which value is actually there. 您需要检查HasLeft以查看实际存在的值。 One could use just an object field and some casting which I prefer to avoid in a statically typed language. 人们可以只使用一个对象字段和一些我宁愿用静态类型语言避免的转换。 There are other possible solutions too (using dynamic or struct ) but this (IMHO) will do. 还有其他可能的解决方案(使用dynamicstruct )但这个(恕我直言)会这样做。

Note: One other way is to derive each case class from a base class . 注意:另一种方法是从base class派生每个case class base class The bad thing (IMHO again) about that is you have to provide a separate overloaded functions for each case class . 关于这一点的坏事(恕我直言)是你必须为每个case class提供一个单独的重载函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM