如何访问泛型类中泛型成员的属性 [英] How to access Property of generic member in generic class

查看:60
本文介绍了如何访问泛型类中泛型成员的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手.我正在尝试创建一个通用类.我有3个班级和一个Main/Generic班级.

I am newbie in C#. I am trying to create a Generic class. I have three classes and a Main/Generic class.

三个班级

public class A
{
    public string Name { get; set; }
    public string Address { get; set; }

    public A(string _name, string _address)
    {
        Name = _name;
        Address = _address;
    }        
}

public class B
{
    public string Name { get; set; }
    public string Address { get; set; }

    public B(string _name, string _address)
    {
        Name = _name;
        Address = _address;
    }
}

public class C
{
    public string Name { get; set; }
    public string Address { get; set; }

    public C(string _name, string _address)
    {
        Name = _name;
        Address = _address;
    }
}

通用类

public class GenericClass<T>
{
    public GenericClass(T obj)
    {
        DynamicObject = obj;
    }

    public T DynamicObject { get; set; }

}

我已经成功创建了一个通用类.

I have successfully created a Generic class.

class Program
{
    static void Main(string[] args)
    {
        A objA = new A("Mohit", "India");
        GenericClass<A> objGenericClass = new GenericClass<A>(objA);

        Console.ReadLine();
    }

}

现在,如果我需要在Generic类中使用Class A/B/C属性.我该如何使用?我知道类引用类型决定运行时.所以,我不能以下面的方式使用它.但是,还有其他方法吗?

Now, if I need to use Class A/B/C property in the Generic class. How can I use it? I know that class reference type decide on the runtime. So, I can't use it in below way.But, Is there any other way?

public class GenericClass<T>
{
    public GenericClass(T obj)
    {
        DynamicObject = obj;
    }

    public T DynamicObject { get; set; }


    public void UseClassPro()
    {
        Console.WriteLine("Address " + DynamicObject.Address);//Compile time error here.
    }

}

推荐答案

其他答案正确,但是...

我只想指出:尽管这些其他答案促进了有效的C#代码,但它们使实现的通用方面变得多余.您不再需要泛型:

The Other Answers are right, but...

I just want to point out: while those other answers promote valid C# code, they make the generic aspect of you implementation superflous. You don't need generics anymore:

给出基类或接口,如

public interface IHasAddress
{
    string Address { get; }
}

您不再想要一个泛型类(您可以通过提供的代码告诉我):

you don't need a Generic class anymore for what you are trying to achive (from what i can tell by the code you provided):

public class NotSoGenericClass
{
    public GenericClass(IHasAddress obj)
    {
        DynamicObject = obj;
    }

    public IHasAddress DynamicObject { get; set; }    
}

因此,如您所见,无需泛型即可轻松实现所需的行为.

So as you can see, you can easily implement the desired behaviour w/o generics.

对于初学者,在泛型方面,我建议您遵循以下基本规则:

For you as a Beginner, i'd recommend the following basic rules when it comes to generics:

  1. 当您认为必须使用泛型时,请强制自己首先考虑通过接口,抽象类或基类进行抽象.这通常会导致更简单,更清洁的解决方案.
  2. 反思也一样.当您认为自己需要反思时,请考虑泛型(此时规则1有效)

但是泛型没什么不对的,它只是更复杂,通常不需要.将上面的类与通用解决方案进行比较:

But Nothings wrong with Generics, its just more complex and often not needed. Compare the class above with the generic solution:

public class GenericClass<T> where T : IHasAddress  // just for the sake of generics
{
    public GenericClass(T obj)
    {
        DynamicObject = obj;
    }

    public T DynamicObject { get; set; }    
}

看起来更复杂并且没有增加任何好处,对吗?还要注意,无论如何,您都需要一个接口/基类.否则,您也可以使用反射(不推荐).

Looks more complex and doesn't add any benefit, does it? Also note that you need a Interface/baseclass no matter what. Otherwise, you could also use Reflection (not recommended).

实际回答您的问题

您问题的准确答案是:

您必须定义必须使用

public class GenericClass<T> where T : IHasAddress
                             ^^^^^^^^^^^^^^^^^^^^^
                                  This Part

通过这种方式,编译器知道 T 是继承的或类型为 IHasAddress 或您定义的类型.您还可以在此位置传递多种类型,这在设计界面时增加了灵活性.

This way, the compiler knows that T inherits or is of type IHasAddress or what ever you define. You can also pass multiple types at this place which adds mor flexibility when it comes to designing your interfaces.

也许您的用例中有一些要考虑的要点,从您在问题中提供的信息来看,这些要点并不明显.在这种情况下,请随时添加一些细节,我也很乐意深入探讨这些细节.

Maybe, there are points to consider in your usecase which are not obvious from the information you provided in the question. In that case, feel free to add some details and i'll be happy to deep dive into those as well.

这篇关于如何访问泛型类中泛型成员的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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