110,535
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
}
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);
*/
#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);
IEmployee<Personal> obj = (IEmployee<Personal>) (assmebly.CreateInstance(paraT.FullName)); //创建其实例
我想要实现的是 类型不固定 。所以不能直接<Personal>,这个Personal类型是从外部传过来的参数(类型名称),从程序集中取到的的。
0
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)); //创建其实例