【c#-声明具有限制类型的泛型字典】教程文章相关的互联网学习教程文章

func上的C#可选参数丢失泛型类型推断【代码】

我有琐碎的通用枚举解析方法:public static T Parse<T>(string value) => (T)Parse(typeof(T), value, false);用法是:public IEnumerable<DocumentTypesEnum> FileTypeEnums => FileTypes.Split(',').Select(Extensions.StringEnumerator.Parse<DocumentTypesEnum>);以上编译很好. 将可选参数添加到Parse< T>:public static T Parse<T>(string value, bool ignoreCase = false) => (T)Parse(typeof(T), value, ignoreCase);现在...

c# – 在泛型类上使用“partial”【代码】

大家好,我正在使用一个名为ViewModelCollection< BaseViewModel>的泛型类.它处理ViewModel列表并提供标准的add()和delete()命令. 现在我想知道我是否可以使用部分构造为某个ViewModel“扩展”这个类,其名称是CarViewModel. 这样的事情可能吗?partial class ViewModelCollection<BaseViewModel> {... some command and list stuff ... }partial class ViewModelCollection<CarViewModel> {... special commands for car view model...

C#在接口及其实现者类中使用泛型【代码】

我想创建一个适用于所有IComparable类型的接口.例如public interface SortAlgorithm<T> where T : System.IComparable<T> {List<T> Sort(List<T> input); }我希望它的实现者也是通用的,并且我在界面中提供了相同的规范.像下面的例子public class InsertionSort<T> : SortAlgorithm<T>这是我这样做的目的.我希望我的所有排序算法都适用于实现IComparable接口的所有类型.并且希望在接口中提供T是IComparable的子类的规范.但是当我这样...

c# – 具有接口约束和类实现接口的泛型类【代码】

最近我实现了一个Trie的数据结构,并决定节点可以存储不同类型的数据或者它的实现方式不同,所以我去了Node< T>.然后当我进入构建Trie的算法时,我意识到它需要更多关于Node的知识,所以我限制泛型类使用INode接口.这允许更大的灵活性,但在泛型类的上下文中感觉错误. 通用类对实现接口的类具有不同的用例.例如,List< T> – 算法可以在不依赖于相关抽象集的情况下工作.实现接口的类可能需要多态/ DI,但接口将更加专业化. 在什么情况下其...

c# – String泛型参数是否被视为值类型或引用类型?

从MSDN Generics in the Run Time文件我注意到:When a generic type is first constructed with a value type as a parameter, the runtime creates a specialized generic type with the supplied parameter or parameters substituted in the appropriate locations in the MSIL. Specialized generic types are created one time for each unique value type that is used as a parameter.和Generics work somewhat differentl...

C#:扩展泛型类【代码】

partial class Repository<TEntity> : IRepository<TEntity> where TEntity : class { }我的通用存储库为TEntity实现了一组通用的方法public TEntity Get(int id) {return _context.Set<TEntity>().Find(id); }public TEntity Get(Expression<Func<TEntity, bool>> predicate) {return _context.Set<TEntity>() }我可以访问Repository<User>().Get();许多存储库执行相同的操作集,因此它很有用,但现在我想扩展Repository< User>支持...

接收List的C#泛型方法不会为实际类型的T调用重载方法(更喜欢通用类型)【代码】

参见英文答案 > why is this generic not resolved at compile time? 4个我有这个例子C#代码:class Stuff { } // empty classvoid Main() {var list = new List<Stuff> {new Stuff(),new Stuff()};Fun(list); }void Fun<T>(List<T> a) {Debug.Log("called List<T> Fun");foreach (T t in a) {Fun(t);} }void Fun(Stuff a) {Debug.Log("called Stuff Fun"); }void Fun<T>(T a) {Debug.Log("ca...

泛型c#.net【代码】

在下面的代码“where T:WsgTypes.RouteRestriction”中,我可以添加多个类,以便T只能是我感兴趣的那几个类类型public static T GetDetails<T>(string code) where T : WsgTypes.RouteRestriction{T details;if (typeof(T) == typeof(WsgTypes.TicketType)){details = TicketTypeDetail.GetDetails(code) as T;}else if (typeof(T) == typeof(WsgTypes.RouteRestriction)){details = RouteRestrictionDetail.GetDetails(code) as T;...

c# – 泛型类型的奇怪行为【代码】

请看下面的简单代码public class A{}public class B: A{}public class G<T> where T : A{public T GetT(){return new A();}}此代码不正确 – 编译器错误“无法将A转换为返回类型T”.但A实际上是T.如果我改变了return new A(); 至return new A() as T;一切都好.这种行为的原因是什么?提前致谢 UPD:初始问题出错.现在修好了解决方法:根据更新重新设计的答案 虽然A符合T:A的通用约束,但它是一种具体类型.但是,您的泛型类的GetT()方...

c#泛型. ,以编程方式从读取字符串值中分配T?【代码】

我有两个类(即Customer和Employee)和一个通用存储库GenericRepository< T>哪里T:上课. 是否可以在从字符串中分配T的值时实例化新的GenericRepository实例? 像这样:string x = "Customer"; var repository = new GenericRepository<x>();(从而创建GenericRepository类型的存储库实例< Customer>)解决方法:是的,但这很尴尬.string name = "MyNamespace.Customer";Type targetType = Type.GetType(name);Type genericType = typeo...

C#泛型可以这么酷吗?【代码】

我希望能够做到这样的事情:class A<T1, T2> {public void Call(T1 arg1, T2 arg2){B b = new B();b.DoSomething(arg1); // determine which overload to use based on T1b.DoSomething(arg2); // and T2} } class B {public void DoSomething(int x){// ...}public void DoSomething(float x){// ...} }我知道可以通过if / else检查来完成,但这似乎并不优雅,特别是当我有20种类型可供选择时.解决方法:如果你想要类型安全,你可以...

c# – 为什么我必须在泛型类上使用静态方法调用【代码】

我遵循this泛型类的例子.因为我不想用测试代码填充我的项目的主要功能,所以我想创建一个运行代码示例的静态展示函数. 我的代码:namespace Syntax {public class GenericClass<T>{private class Node{private T data;private Node next;public Node(T t){next = null;data = t;}public Node Next { get { return next; } set { next = value; } }public T Data { get { return data; } set { data = value; } }}private Node head;...

c# – 如何在泛型方法中将泛型集合指定为限制【代码】

阿罗哈 我有一个带有(伪)签名的方法:public static T Parse<T>(string datadictionary) where T : List<U>这不构建.如何限制方法只接受通用List<>对象(应该是cource不包含T的但是其他的东西:) 我需要限制T的类型,因为我需要在此代码中调用它的方法.传入的类型是自定义集合(基于List).public class MyCollection<T> : List<T> where T: MyClass, new() {public void Foo(); }public static T Parse<T>(string datadictionary) wher...

c# – 使用泛型回调是不好的形式?【代码】

我有这个代码(好吧,类似的东西).private delegate void GenericCallback<T>(T Info);private void DoWork() {System.Threading.Thread Worker = new System.Threading.Thread(delegate() {TestMethod(TestMethodCallback<string>);});Worker.Start(); }private void TestMethod(GenericCallback<string> Callback) {System.Threading.Thread.Sleep(1000);if(Callback != null){Callback("Complete");} }private void TestMethod(Ge...

如何使用C#中的反射查找实现泛型抽象类的所有类?【代码】

我有一个看起来像这样的c#类public abstract class Listener<T> where T : Event {public abstract void Handle(T _event); }我将这个类扩展为这样的东西public class SendWelcomeEmail : Listener<UserWasCreated> {public override void Handle(UserWasCreated _event){//...} }我需要使用反射来查找扩展Listener的所有类<>基类. 我尝试了以下内容var listeners = AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly ...