C#键入Generic类 [英] C# Type to Generic class

查看:64
本文介绍了C#键入Generic类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个MVC C#代码优先的项目。我想将类型传递给我的方法。我的方法需要上课才能工作但是我不想通过课我想要动态。我不知道是不是可能?



这是我的代码;



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;

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?







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

                         //TODO
}

推荐答案

你想要类型是TEntity吗?



You want type to be of TEntity?

public class LINQDynamic<TEntity> where TEntity: class
       {

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

                         //TODO
}





或其他已知类型?





or a different known type?

public class LINQDynamic<TEntity> where TEntity: class
       {

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

                         //TODO
}


这篇关于C#键入Generic类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆