简体   繁体   English

在C#中如何在泛型类中实现typeof(entity)

[英]in c# how to implement typeof(entity) inside generic class

what i want to achieve : 我想实现的目标:

Type type = typeof(Entity);
var result =_unitofwork.BaseRepositoryFor<type>.createEntity(entity);

I have tried method info and few more examples but couldnt endup working. 我已经尝试了方法信息和更多示例,但最终无法正常工作。

CreateEntity/deleteEntity all back end stuffs are implemented generically butI need to add somehow the type inside my generic method. CreateEntity / deleteEntity所有后端的东西都是通用实现的,但是我需要在通用方法中添加某种类型。

what is working fine : 什么工作正常:

MyDb _db= new MyDb();
private static T entity;
private static BaseRepositoryFor<T>_dynamic_class = new BaseRepository<T>(_db);

var result =_unitofwork.BaseRepositoryFor<T>.createEntity(entity);

But I need to Type object instead of 'T' in my method. 但是我需要在我的方法中输入对象而不是'T'。

Duplicate ISSUE :I already checked that duplicate question. 重复的问题:我已经检查了那个重复的问题。 but it dint actually solved it my problem since i have some extension methods to create/delete/update/get and i need to get those methods name in intelligence. 但是它确实解决了我的问题,因为我有一些扩展方法可以创建/删除/更新/获取,而且我需要在智能中获取这些方法的名称。

I believe what you need is MakeGenericType : 我相信您需要的是MakeGenericType

var t = typeof(BaseRepositoryFor<>)
Type constructed = t.MakeGenericType(inferedType);

Now you can call the actual method by reflection: 现在您可以通过反射调用实际方法:

var m = constructed.GetMethod("createEntity");
var result = m.Invoke(null, new[] { entity });
public Type GetSome<T>(T intake)
{
            T localT = default(T);

}

I believe that's what you are looking for. 我相信这就是您想要的。 And from the point you can do whatever you need with localT 从这一点localT您可以使用localT做任何您需要做的事情

You have to create a generic method in your class. 您必须在类中创建一个通用方法。 If possible, call it with the generic parameter from outside. 如果可能,请从外部使用通用参数进行调用。 Alternatively, you can call the generic method via reflection, however note that this is slow. 另外,您可以通过反射调用泛型方法,但是请注意,这很慢。 I give you a very primitive, not very maintainable example below, but you will see the point: 我在下面给您提供一个非常原始的,不是很容易维护的示例,但是您会明白这一点:

private TEntity MyMethodGeneric<TEntity>(){
    return _unitofwork.BaseRepositoryFor<TEntity>.createEntity(entity);
}

private object MyMethod(Type type){
    var method = GetType().GetMethod("MyMethodGeneric", BindingFlags.Instance|BindingFlags.NonPublic);
    return method.MakeGenericMethod(type).Invoke(this, new object[]{/* if you need to pass parameters}*/);
}

Again, I would try to avoid this if you can. 同样,如果可以的话,我会尽量避免这种情况。 Pass in the generic parameter at a higher level. 在更高级别传递通用参数。 If you absolutely have to use this pattern, you should see if you can make it better maintainable, eg by not hard-coding the method name or even by caching the actual methods ( MakeGenericMethod is very slow). 如果绝对必须使用此模式,则应查看是否可以使其更好地维护,例如,通过不对方法名称进行硬编码,甚至不缓存实际的方法( MakeGenericMethod速度非常慢)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM