0

I have a project in MVC C# code-first. I want to pass a type to my method. And my method needs a class to work. But I don't want to pass a class - I want it to be dynamic. Is it possible or not?

Here is my codes;

string item ="PERSON" // item is a parametric I give the table name to get the type of table 
Assembly asm = Assembly.Load("ProjectName");
Type entityType = asm.GetType(String.Format("NameSpace.Model.{0}", item));

 var test = LINQDynamic<>.GetQueryResult(ssa,entityType,ddd); // In LINQDynamic<> I want to use entityType like that LINQDynamic<entityType> but it's not acceptable what can I make the convert type to generic?..Because table name is parametric.Is it possible to convert that type?

public class LINQDynamic<TEntity> where TEntity: class
   {

       public static object GetQueryResult(object pkKey,Type type, params object[] pkKeys)
       {
                     //TODO
       }
    }
EluciusFTW
  • 2,565
  • 6
  • 42
  • 59
Bora Oktekin
  • 33
  • 1
  • 6
  • 2
    Possible duplicate of [How to use reflection to call generic Method?](http://stackoverflow.com/questions/232535/how-to-use-reflection-to-call-generic-method) – sara Oct 07 '15 at 06:34
  • Yes may be it's same.But my table name is dynamic.How can I convert or how can I use the reflection for create string table name to real class passing to LINQDynamic. – Bora Oktekin Oct 09 '15 at 13:03

4 Answers4

0

Reflection example:

public static void Main(string[] args)
{
    Type t = typeof(string);
    Type fooType = typeof(Foo<>);
    Type genericFooType = fooType.MakeGenericType(t);
    object o = Activator.CreateInstance(genericFooType,null);
    MethodInfo method = genericFooType.GetMethod("Bar");
    method.Invoke(o, null);
}

public class Foo<T> where T:class
{
    public void Bar()
    {
        Console.WriteLine ("Calling Bar");
    }
}
blogbydev
  • 1,445
  • 2
  • 17
  • 29
0

It seems that you are not using the TEntity in your LINQDynamic class - instead you pass the type as a parameter. This makes the TEntity kind of pointless.

Try:

public class LINQDynamic
{
   public static object GetQueryResult(object pkKey,Type type, params object[] pkKeys)
   {
                 //TODO
   }
}

var test = LINQDynamic.GetQueryResult(ssa,entityType,ddd);
Dietz
  • 578
  • 5
  • 14
0

I've run into the same problem before. Here's is generally what I do. Keeping winding your way up the call stack in your code until you get to a point where you can describe what's happening with an interface and not a generic, i.e.; IEnumerable GetResults(string item).

Then, code gen the contents of the method (this allows you to keep the generic method call) using Expression Trees, and store the compiled lambda in a static dictionary, where the key is your "item" and the value is Func < IEnumerable < IEntity > >.

Tread lightly though. It's easy to run into issues with multi-threading, memory leaks, and other nasty problems in concurrent and long-running applications like web applications. I would almost recommend you not do it.

Kyle Pena
  • 517
  • 2
  • 8
0

Adapted the suggestion by singsuyash:

  object[] newobj = { pkey,entityType,pKyes };
            object[] parameters = new object[] { newobj };
            Type t = typeof (string);
            Type linqType = typeof (LinqDynamic<>);
            Type genericLinqType = linqType.MakeGenericType(entityType);
            object o = Activator.CreateInstance(genericLinqType, null);
            MethodInfo method = genericLinqType.GetMethod("GetEntityByPrimaryKey");
            var results = method.Invoke(o, parameters);
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Bora Oktekin
  • 33
  • 1
  • 6