如何声明一个通用基类来操作值和实例? [英] How to declare a generic base class to manipulate values and instances ?

查看:181
本文介绍了如何声明一个通用基类来操作值和实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我正在尝试形成一个基类来管理值类型的值和类的实例.

为此,我尝试了以下操作:

Hello Everyone

I am trying to form a base class to manage values of value-types and instances of classes.

To do that I tried the following:

public interface IBase<T>
{
    T Value
    { get; set; }
}

public abstract class Base<T> : IBase<T>
{
    private T value;
    public T Value
    {
       get { return value; }
       set { this.value = value; }
    }
}

public class StringBase : Base<string>
{
  public StringBase(string value)
  {
     this.Value = value;
  }
}

public class IntBase : Base<int>
{
  public IntBase(int value)
  {
     this.Value = value;
  }
}

public class MyClassEntity : Base<MyAnyClass>
{
   public MyClassEntity(MyAnyClass instance)
   {
      this.Value = instance;
   }
}



然后在某些代码中我想这样做



then in some code I want to do this

StringBase sb = new StringBase("Test");
IntBase ib = new IntBase(3);

dreambaseIamlookingfor x = sb;
System.Diagnostics.Debug.WriteLine(x.Value);
x = ib;
x.Value = 30;
System.Diagnostics.Debug.WriteLine(x.Value);



例如,我也不能这样做:



For example I cannot do this also:

 List<dreambaseIamlookingfor> lst = new List<dreambaseIamlookingfor>();
lst.Add(sb);
lst.Add(ib);



我有可能建立这样的梦想基地吗?
如果是,那么"dreambaseIamlookfor"类或接口可以是什么,怎么可能?

我不知道在这种情况下协方差或协方差对我有帮助吗?

谢谢



Does it possible that I can form such a dream base ?
If it is, then what can be and how can be ''dreambaseIamlookingfor'' class or interface ?

I do not know if covariance or contravariance will help me in this case ?

Thank you

推荐答案

安迪.嗨.

我明白了您的意思,但我对C#和.NET的进展以及我在有关协方差和逆方差的阅读范围内的理解不太清楚.我认为这是可能的.

但是,如果我不介意在类层次结构的基础上使用泛型,那真的很简单.我可以更改我的IBase< T>到IBase,并使该接口获取并设置一个Object值而不是T,我可以将其转换为T,反之亦然.我试图避免这种情况.我相信这可能会导致性能下降.最后,我现在知道使用对象类型是不可避免的.

谢谢和最诚挚的问候,
Erhan
Hi Andi.

I see your point, but being not much clear to me, the progress of C# and .NET and within the scope of my readings about covariance and contravariance I thought it could be possible.

It is really simple though if I do not mind to use generics at the very base of my class hierarcy. I could just change my IBase<T> to IBase and make this interface to get and set an Object value instead of T and I can convert this to T and vice versa. I tried to avoid this. I believe this could cause performance penalties. At the end I now know that it is unavoidable for me to use Object type.

Thanks and best regards,
Erhan


泛型和多态是两个正交的概念.

您尝试在具有通用类的地方使用多态性.
例如. x.value显然不是多态的,因为它根据某些泛型类型具有单独的实现.

如果您有能力进行松散的编译时类型检查并愿意为反射付出运行时开销,并且如果您也愿意在运行时处理链接异常,则可以将x定义为dynamic并将list定义为List< dynamic>. br/>
我一般不建议这样做.

您应该决定是需要多态还是需要泛型,但同时不能同时解决您遇到的问题.

如果要将泛型与多态混合在一起,则对于多态访问,必须仅使用没有泛型类型的方法没有泛型属性.

安迪

修改后的答案:
示例代码可能会显示如何使用扩展方法来使用具有泛型的一种接口方法.如您所见,由于需要了解多态对象的类型,所以多态被破坏了:

Generics and polymorphism are two orthogonal concepts.

You try to use polymorphism where you have generic classes.
E.g. x.value is obviously not polymorphic since it has separate implementations depending on some generic types.

If you can afford to loose compile time type checks and are willing to pay the runtime overhead for reflection and if you are also willing to handle linking exceptions at runtime, then you could define x as dynamic and the list as List<dynamic>.

I would generally not advise that, though.

You should decide if you need polymorphism or if you need generics, but not both for your problem at hand.

If you want to have generics mixed with polymorphism, then for polymorphic access, you must only employ methods without generic types and no generic properties.

Andi

Edited Answer:
Example code that might show how you may employ extension methods for a kind of interface methods with generics. As you can see, polymorphism is broken since you need to know the type of the polymorphic object:

public static class Extension
{
    public static bool Is<T>(this IBase obj) { return obj is Base<T>; }
    public static Base<T> As<T>(this IBase obj) { return (Base<T>)obj; }
}

public interface IBase
{
}

public class Base<T> : IBase
{
    public T Value { get; set; }
}

public class StrBase : Base<string>
{
}

public class IntBase : Base<int>
{
}

class Program
{
    static void Main(string[] args)
    {
        {
            StrBase sb = new StrBase();
            sb.Value = "abc";
            string s = sb.Value;
        }

        {
            IntBase ib = new IntBase();
            ib.Value = 123;
            int i = ib.Value;
        }

        {
            IBase obj = new IntBase();

            obj.As<int>().Value = 234;
            int i = obj.As<int>().Value;
        }

        {
            List<IBase> list = new List<IBase>();
            list.Add(new IntBase());

            list[0].As<int>().Value = 123;
            int i = list[0].As<int>().Value;
        }
    }
}


这篇关于如何声明一个通用基类来操作值和实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆