Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

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

Here is my codes;

C#
strimg 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?


C#
public class LINQDynamic<TEntity> where TEntity: class
       {
           
           public static object GetQueryResult(object pkKey,Type type, params object[] pkKeys)
           {

                         //TODO
}
Posted

1 solution

You want type to be of TEntity?

C#
public class LINQDynamic<TEntity> where TEntity: class
       {

           public static TEntity GetQueryResult(object pkKey,TEntity type, params object[] pkKeys)
           {

                         //TODO
}


or a different known type?

C#
public class LINQDynamic<TEntity> where TEntity: class
       {

           public static TOther GetQueryResult<TOther>(object pkKey,Tother type, params object[] pkKeys)
           {

                         //TODO
}
 
Share this answer
 
v2
Comments
bora1891 7-Oct-15 2:45am    
My problem is here;
var test = LINQDynamic<-?->.GetQueryResult();

which is the question mark.I want to pass my Type which is entityTpe.Is there a way to pass my entityType variable?
Duncan Edwards Jones 7-Oct-15 5:05am    
If the type manager can't derive it (like here where you have no explicit parameters of TEntity) you can explicitly state it. e.g. to get a guery result of Customer:-

var test = LINQDynamic<customer>.GetQueryResult();

You should also make the return type of GetQueryResult be TEntity rather than object.

public class LINQDynamic<tentity> where TEntity: class
{

public static TEntity GetQueryResult(object pkKey,TEntity type, params object[] pkKeys)
{

//TODO
}
bora1891 7-Oct-15 8:48am    
Thank you for reply.
But I think I cannot get the "Class" name. If i get the class name (like when i write the class name statistically as an input) this part will work:
public class LINQDynamic<-TEntity-> where TEntity: class
{

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

So I am even not sure that I can get the class name from table type.And maybe I make some conversition type to real class but I don't know how to make and I'm notsure this is possible becasue my entityType is dynamic.
The LINQDynamic, wait a class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900