简体   繁体   English

如何在C#中为泛型类创建别名?

[英]How can I create an alias for a generic class in C#?

How can I do the following in C#? 如何在C#中执行以下操作? What is the right way to write the first line of this code snippet? 编写此代码段的第一行的正确方法是什么?

using KVP<K, V> = System.Collections.Generic.KeyValuePair<K, V>;

class C { KVP<int, string> x; }

You can't, basically. 基本上你不能。 You can only use fixed aliases, such as: 您只能使用固定别名,例如:

using Foo = System.Collections.Generic.KeyValuePair<int, string>;

class C { Foo x; }

In some cases you can go with inheritance: 在某些情况下,您可以继承:

public class MyList<T1, T2> : List<Tuple<IEnumerable<HashSet<T1>>, IComparable<T2>>> { }

public void Meth()
{
    var x = new MyList<int, bool>();
}

Though not in your particular case, as KeyValuePair is sealed :-( 虽然不是在你的特定情况下,因为KeyValuePair是密封的:-(

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

相关问题 C#:全局别名是通用类名吗? - C#: Globally alias a generic class name? 如何在C#中找到泛型类的属性? - How can I find the attributes of a generic class in C#? 如何在C#和DataAnnotation中创建通用的UniqueValidationAttribute? - How can I create a generic UniqueValidationAttribute in C# and DataAnnotation? 如何在 C# LINQ 中创建一个通用的 Join() - How can I create a generic Join() in C# LINQ C#我可以在返回不同泛型类的非泛型类中创建泛型方法或属性吗? - C# Can I create a generic method or property inside a non-generic class that returns a different generic class? C#-如何在“通用类”类型的MainForm类中声明变量而不指定通用类型 - C# - How can I declare a variable in MainForm class of type “Generic Class” without specifing generic type 创建通用类C# - Create Generic Class C# 如何在 C# 中使用 office 365 API 创建群组邮件别名 - How can I create group mail alias using office 365 API in C# 通过从C#中的泛型类派生来创建类型别名 - Creating a type alias by deriving from a generic class in C# C#泛型类,如何使用类型参数的派生类的公共属性? - C# Generic Class, how can I use public properties of derived class of type parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM