0

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.

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.

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.

APALALA
  • 60
  • 9
  • 1
    You're question is a little unclear, but the correct syntax for your code snippet is `_unitofwork.BaseRepositoryFor`. If your method is also generic, with a generic parameter called `T`, then `_unitofwork.BaseRepositoryFor`. – RB. Feb 01 '16 at 15:15
  • my method is generic and I can not use in my method because the user request would be received as Type type= typeof(something); and I need to work with 'type' in my generic method – APALALA Feb 01 '16 at 15:20
  • 1
    If your type is T, you can just pass it down `var result =_unitofwork.BaseRepositoryFor.createEntity(entity);` – wertzui Feb 01 '16 at 15:23
  • 2
    Possible duplicate of [How do I use reflection to call a generic method?](http://stackoverflow.com/questions/232535/how-do-i-use-reflection-to-call-a-generic-method) – MakePeaceGreatAgain Feb 01 '16 at 15:23
  • no my type is not T unfortunately – APALALA Feb 01 '16 at 15:31
  • You are going to need to post some more code in that case, because what you've stated sounds wrong. The type of `entity` and the type of `T` would be the same thing in a sane scenario! – RB. Feb 01 '16 at 15:33
  • T is a generic type, so in this case typeof(T) == typeof(Entity) if you do something like this _unitofwork.BaseRepositoryFor.createEntity(entity); then T becomes Entity – Donald Jansen Feb 01 '16 at 15:35

3 Answers3

1

I believe what you need is 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 });
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
0
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

Anatolyevich
  • 671
  • 5
  • 14
0

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, e.g. by not hard-coding the method name or even by caching the actual methods (MakeGenericMethod is very slow).

Roland Buergi
  • 1,157
  • 9
  • 23