相关疑难解决方法(0)

C#中的泛型,使用变量类型作为参数

我有一个通用的方法

bool DoesEntityExist<T>(Guid guid, ITransaction transaction) where T : IGloballyIdentifiable;
Run Code Online (Sandbox Code Playgroud)

如何以下列方式使用该方法:

Type t = entity.GetType();
DoesEntityExist<t>(entityGuid, transaction);
Run Code Online (Sandbox Code Playgroud)

我一直收到愚蠢的编译错误:

找不到类型或命名空间名称't'(您是否缺少using指令或程序集引用?)

DoesEntityExist<MyType>(entityGuid, transaction);
Run Code Online (Sandbox Code Playgroud)

完美的工作,但我不想使用if指令每次调用具有单独类型名称的方法.

.net c# generics types

120
推荐指数
4
解决办法
14万
查看次数

如何从C#中的字符串创建泛型类?

我有一个像这样的Generic类:

public class Repository<T> {...}
Run Code Online (Sandbox Code Playgroud)

我需要用字符串来实例...示例:

string _sample = "TypeRepository";
var _rep = new Repository<sample>();
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?这甚至可能吗?

谢谢!

.net c# generics reflection

16
推荐指数
2
解决办法
2万
查看次数

标签 统计

.net ×2

c# ×2

generics ×2

reflection ×1

types ×1