如何传递动态类型到TModel类型的泛型参数中?

qq874655551 2018-12-11 01:22:06

想要实现的功能是:传递一个不能确定的类型dynamicType(例如类型名称:"Personal") ,根据此类型执行一些操作(Create),但是C#不允许将dynamicType变量当成类型传递给TModel,提示说无法将变量当成类型使用。有没有什么办法将代码能够正确运行?


class Program
{
static void Main(string[] args)
{
#region 具体类型可传递。
Personal specifiedPersonal = new Personal();

Employee<Personal> employee = new Employee<Personal>();
employee.Create(specifiedPersonal);
#endregion


#region 动态类型传递到TModel。报错。
Assembly assmebly = typeof(Personal).GetTypeInfo().Assembly;
Type dynamicType = assmebly.ExportedTypes.Where(x => x.Name == "Personal").FirstOrDefault();
Employee<dynamicType> dynamicEmployee = new Employee<dynamicType>();//无法将变量当成 类型 使用

dynamicEmployee.Create(dynamicEmployee);
#endregion

Console.ReadLine();
}
public class Personal
{
public string FirstName { get; internal set; }
public string LastName { get; internal set; }
}
public interface IEmployee<TModel>
{
Guid Create(TModel model);
bool Update(TModel model);
}

public class Employee<TModel> : IEmployee<TModel>
{
public Guid Create(TModel model)
{
// TODO :
return Guid.NewGuid();
}
public bool Update(TModel model)
{
// TODO :
return true;
}
}
}
...全文
294 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq874655551 2018-12-11
  • 打赏
  • 举报
回复
引用 5 楼 正怒月神 的回复:
这个意思?
#region 动态类型传递到TModel。报错。
            //假设传递一个a进来
            Personal a = new Personal();
            var type = typeof(Employee<>).MakeGenericType(a.GetType());
            dynamic a_Context = Activator.CreateInstance(type);
            var q =  a_Context.Create(a);
            Console.WriteLine(q);
            Console.WriteLine();
            

            #endregion
这里的代码描述不太好。我重新发帖了:https://bbs.csdn.net/topics/392491944
qq874655551 2018-12-11
  • 打赏
  • 举报
回复
引用 5 楼 正怒月神 的回复:
这个意思?
#region 动态类型传递到TModel。报错。
            //假设传递一个a进来
            Personal a = new Personal();
            var type = typeof(Employee<>).MakeGenericType(a.GetType());
            dynamic a_Context = Activator.CreateInstance(type);
            var q =  a_Context.Create(a);
            Console.WriteLine(q);
            Console.WriteLine();
            

            #endregion
引用 1 楼 水边2 的回复:
你要用反射去创建实例,例如:

Assembly assmebly = typeof(Personal).GetTypeInfo().Assembly;
Type type = assmebly.GetType("Mike.ConsoleApp.Program+Employee`1");
Type paraT = type.MakeGenericType(typeof(Personal));  //指定泛型类
IEmployee<Personal> obj = (IEmployee<Personal>) (assmebly.CreateInstance(paraT.FullName));  //创建其实例


引用 6 楼 likelinsiyuan 的回复:
哦,我看错了

string dllpath = @"程序集绝对路径";
Assembly assembly = Assembly.LoadFrom(dllpath);
Type type = assembly.GetType("DynamicClassLibrary.Personal");
Employee<Type> dynamicEmployee = new Employee<Type>();
dynamicEmployee.Create(type);
/*
Create方法中获取实例
Object obj = Activator.CreateInstance(type);
*/
举的实例不太恰当,与本来想描述的问题有点偏了,帖子不能修改,就在这了引用下吧。 我是想实现下面方法 :传递动态类型dynamicType,这是通过类型名称取出来的,VS提示是不能把变量当类型使用。因为Employee<TModel>需要的是类型,要没要版本把类型变量转变为类型传递?
namespace Mike.ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            #region Specified Type Transform to TModel type generic parma
            Personal specifiedPersonal = new Personal();

            Employee<Personal> employee = new Employee<Personal>();
            employee.DoSth();
            #endregion

            #region 如何动态传递类型?
            Assembly assmebly = typeof(Personal).GetTypeInfo().Assembly;
            Type dynamicType = assmebly.ExportedTypes.Where(x => x.Name == "Personal").FirstOrDefault();
            Employee<dynamicType> dynamicEmployee = new Employee<dynamicType>();
            dynamicEmployee.DoSth();
            #endregion           

            Console.ReadLine();
        }
        public class Personal
        {
            public string FirstName { get; internal set; }
            public string LastName { get; internal set; }
        }
        public class Employee<TModel>
        {
            public void DoSth()
            {
                Console.WriteLine("Done !!!"); ;
            }
        }
    }    
}
  • 打赏
  • 举报
回复
哦,我看错了

string dllpath = @"程序集绝对路径";
Assembly assembly = Assembly.LoadFrom(dllpath);
Type type = assembly.GetType("DynamicClassLibrary.Personal");
Employee<Type> dynamicEmployee = new Employee<Type>();
dynamicEmployee.Create(type);
/*
Create方法中获取实例
Object obj = Activator.CreateInstance(type);
*/
正怒月神 2018-12-11
  • 打赏
  • 举报
回复
这个意思?
#region 动态类型传递到TModel。报错。
            //假设传递一个a进来
            Personal a = new Personal();
            var type = typeof(Employee<>).MakeGenericType(a.GetType());
            dynamic a_Context = Activator.CreateInstance(type);
            var q =  a_Context.Create(a);
            Console.WriteLine(q);
            Console.WriteLine();
            

            #endregion
  • 打赏
  • 举报
回复
我把你代码中错误的部分改了
  • 打赏
  • 举报
回复

Assembly assmebly = typeof(Personal).GetTypeInfo().Assembly;
Type dynamicType = assmebly.ExportedTypes.Where(x => x.Name == "Personal").FirstOrDefault();
Employee<Type> dynamicEmployee = new Employee<Type>();
dynamicEmployee.Create(dynamicType);
qq874655551 2018-12-11
  • 打赏
  • 举报
回复
引用 1 楼 水边2 的回复:
你要用反射去创建实例,例如:

Assembly assmebly = typeof(Personal).GetTypeInfo().Assembly;
Type type = assmebly.GetType("Mike.ConsoleApp.Program+Employee`1");
Type paraT = type.MakeGenericType(typeof(Personal));  //指定泛型类
IEmployee<Personal> obj = (IEmployee<Personal>) (assmebly.CreateInstance(paraT.FullName));  //创建其实例
IEmployee<Personal> obj = (IEmployee<Personal>) (assmebly.CreateInstance(paraT.FullName));  //创建其实例
我想要实现的是 类型不固定 。所以不能直接<Personal>,这个Personal类型是从外部传过来的参数(类型名称),从程序集中取到的的。 0
游北亮 2018-12-11
  • 打赏
  • 举报
回复
你要用反射去创建实例,例如:

Assembly assmebly = typeof(Personal).GetTypeInfo().Assembly;
Type type = assmebly.GetType("Mike.ConsoleApp.Program+Employee`1");
Type paraT = type.MakeGenericType(typeof(Personal));  //指定泛型类
IEmployee<Personal> obj = (IEmployee<Personal>) (assmebly.CreateInstance(paraT.FullName));  //创建其实例


110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧