具有泛型类型的 C# 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2043939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:56:57  来源:igfitidea点击:

C# Property with Generic Type

c#.netgenerics

提问by youwhut

I have a class:

我有一堂课:

public class class1
{
public string Property1 {get;set;}
public int Property2 {get;set;}
}

Which will be instantiated:

哪个将被实例化:

var c = new class1();
c.Property1 = "blah";
c.Property2 = 666;

So bear with me (I am new to generics), I need another class with a property of a generic type so that Property1 or Property2 can be used to set Property3:

所以请耐心等待(我是泛型的新手),我需要另一个具有泛型类型属性的类,以便 Property1 或 Property2 可用于设置 Property3:

public class Class2
{
public GenericType Property3 {get;set;}
}

I want to be able to:

我希望能够:

var c2 = new class2();
c2.Property3 = c1.Property2 // Any property of any type.

采纳答案by JonH

@bytenik I think the originator is asking that class3 be defined to contain a generic property. That way when he / she has a property from class1 or class2 which in this case is a string / int that class3's property could handle either case.

@bytenik 我认为发起者要求将 class3 定义为包含通用属性。这样,当他/她拥有 class1 或 class2 的属性时,在这种情况下是一个 string/int,而 class3 的属性可以处理任何一种情况。

public class Class3<T>
{
 public T Property3 {get;set;}
}

I think the intent is the poster wants to do this:

我认为意图是海报想要这样做:

Class3.Property3 = Class2.Property2

I think the poster will need to cast it to type T for this to work though.

我认为海报需要将其转换为 T 才能正常工作。

Look at the link that was posted for an example: Making a generic property

查看示例中发布的链接:Making a generic property

Here is what you can do:

您可以执行以下操作:

namespace GenericSO
{
    public class Class1
    {
        public int property1 { get;set;}

    }

    public class Class2<T>
    {
        public T property2 { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Class1 c1 = new Class1();
            c1.property1 = 20;

            Class2<int> c2 = new Class2<int>();

            c2.property2 = c1.property1;
        }
    }
}

Notice how your template property2 gets the value of property1. You have to tell it what kind of generic.

请注意您的模板 property2 如何获取 property1 的值。你必须告诉它什么样的泛型。

回答by Vitaliy Liptchinsky

public class class1<T>
{
public T Property3 {get;set;}
}

Regarding to edited version of the question:

关于问题的编辑版本:

If you need a property, which can be set with any type, the most reasonable solution here is to simply use property of type Object. For C# compiler there is no way to find out instance of which exactly type you've previously pushed into property setter.

如果你需要一个可以设置为任何类型的属性,这里最合理的解决方案是简单地使用 Object 类型的属性。对于 C# 编译器,无法找出您之前推送到属性设置器中的确切类型的实例。

回答by Matt Breckon

I think you may have misunderstood generics. Another word that could be used is "template" but that is avoided because it is used for more advanced things in C++.

我想你可能误解了泛型。另一个可以使用的词是“模板”,但要避免使用,因为它用于 C++ 中更高级的东西。

The following will create a generic class of a currently undefined type T.

下面将创建一个当前未定义类型 T 的泛型类。

public class Class2<T>
{
    public T Property3 { get; set; }
}

To use this you need to specify the missing type:

要使用它,您需要指定缺少的类型:

var x = new Class2<int>();

This will create an object that has a property Property3 that is of type int.

这将创建一个具有 int 类型的属性 Property3 的对象。

... or ...

... 或者 ...

var y = new Class2<string>();

This will create an object that has a property Property3 that is of type string.

这将创建一个具有字符串类型属性 Property3 的对象。

From your question I believe you actually want a type where you can assign any type to it at runtime, but this is not what generics provide.

根据您的问题,我相信您实际上想要一种可以在运行时为其分配任何类型的类型,但这不是泛型提供的。