C# 使用C中的泛型将用户定义的类型转换为另一个用户定义的类型#
我试图使用C# 使用C中的泛型将用户定义的类型转换为另一个用户定义的类型#,c#,.net,.net-standard-2.0,C#,.net,.net Standard 2.0,我试图使用Mapper类将一个类转换为另一个类,它适用于非用户定义的类型当涉及到用户定义的类型时,它显示了一个异常 未处理的异常:System.ArgumentException:类型为'DataModal.TblBook'的对象无法转换为类型为'DomainModal.Book'。 在System.RuntimeType.TryChangeType(对象值、绑定器、CultureInfo区域性、布尔需要特殊广播) 位于System.Reflection.MethodBase.CheckArgu
Mapper
类将一个类转换为另一个类,它适用于非用户定义的类型当涉及到用户定义的类型时,它显示了一个异常
未处理的异常:System.ArgumentException:类型为'DataModal.TblBook'
的对象无法转换为类型为'DomainModal.Book'
。
在System.RuntimeType.TryChangeType(对象值、绑定器、CultureInfo区域性、布尔需要特殊广播)
位于System.Reflection.MethodBase.CheckArguments(对象[]参数、绑定器绑定器、BindingFlags invokeAttr、CultureInfo区域性、签名sig)
位于System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(对象对象对象、BindingFlags invokeAttr、绑定器、对象[]参数、CultureInfo区域性)
我曾尝试在运行时使用此答案创建一个新的泛型方法
在TblStudent.cs
中,类型为TblBook
,在Student.cs
中,类型为Book
我无法转换它
但我没能转换它
这是我的Mapper.cs
using DomainModal;
using DataModal;
using System.Linq;
using System;
using System.Reflection;
namespace DataAccessLogic
{
public class Mapper<T, U> where U : new()
{
public U Convert(T input)
{
U newCastType = new U();
var fromObjectProperties = input.GetType().GetProperties();
var toObjectProperties = newCastType.GetType().GetProperties();
foreach (var parentProperty in fromObjectProperties)
{
foreach (var childProperty in toObjectProperties)
{
if (parentProperty.Name == childProperty.Name)
{
childProperty.SetValue(newCastType, parentProperty.GetValue(input));
}
}
}
/*var fromObjectProperties = input.GetType().GetProperties();
foreach (var parentProperty in fromObjectProperties)
{
}*/
return newCastType;
}
}
}
学生会
using System;
namespace DataModal
{
public class TblStudent
{
public string EmailId { get; set; }
public string Password { get; set; }
public string StudentName { get; set; }
public string StudentId { get; set; }
public string PhoneNumber { get; set; }
public TblBook GetBook { get; set; }
}
}
using System;
namespace DomainModal
{
public class Student
{
public string EmailId { get; set; }
public string Password { get; set; }
public string StudentName { get; set; }
public string StudentId { get; set; }
public string PhoneNumber { get; set; }
public Book GetBook { get; set; }
}
}
完整源代码如下:
我不知道我哪里做错了。任何帮助都将不胜感激
谢谢。使用反射递归调用不属于相同
类型(但具有相同名称)的属性上的泛型方法的示例:
公共类映射器
{
公共静态TRes转换(TIn obj)
{
TRes targetInstance=Activator.CreateInstance();
var sourceTypePropertyInfos=obj.GetType().GetProperties();
var targetTypePropertyInfos=targetInstance.GetType().GetProperties();
foreach(sourceTypePropertyInfo中的变量sourceTypePropertyInfo)
{
foreach(targetTypePropertyInfo中的变量targetTypePropertyInfo)
{
if(sourceTypePropertyInfo.Name==targetTypePropertyInfo.Name)
{
if(sourceTypePropertyInfo.PropertyType==targetTypePropertyInfo.PropertyType)
{
targetTypePropertyInfo.SetValue(targetInstance,sourceTypePropertyInfo.GetValue(obj));
}
其他的
{
var sourcePropertyValue=sourceTypePropertyInfo.GetValue(obj);
var methodInfo=typeof(Mapper.GetMethod)(nameof(Mapper.Convert));
var genericMethodInfo=methodInfo.MakeGenericMethod(sourceTypePropertyInfo.PropertyType,targetTypePropertyInfo.PropertyType);
var targetValue=genericMethodInfo.Invoke(new Mapper(),new[]{sourcePropertyValue});
targetTypePropertyInfo.SetValue(targetInstance,targetValue);
}
}
}
}
返回目标姿态;
}
}
称之为:
var student = Mapper.Convert<TblStudent, Student>(tblStudent);
var student=Mapper.Convert(tblStudent);
使用反射递归调用不属于相同类型(但具有相同名称)的属性上的泛型方法的示例:
公共类映射器
{
公共静态TRes转换(TIn obj)
{
TRes targetInstance=Activator.CreateInstance();
var sourceTypePropertyInfos=obj.GetType().GetProperties();
var targetTypePropertyInfos=targetInstance.GetType().GetProperties();
foreach(sourceTypePropertyInfo中的变量sourceTypePropertyInfo)
{
foreach(targetTypePropertyInfo中的变量targetTypePropertyInfo)
{
if(sourceTypePropertyInfo.Name==targetTypePropertyInfo.Name)
{
if(sourceTypePropertyInfo.PropertyType==targetTypePropertyInfo.PropertyType)
{
targetTypePropertyInfo.SetValue(targetInstance,sourceTypePropertyInfo.GetValue(obj));
}
其他的
{
var sourcePropertyValue=sourceTypePropertyInfo.GetValue(obj);
var methodInfo=typeof(Mapper.GetMethod)(nameof(Mapper.Convert));
var genericMethodInfo=methodInfo.MakeGenericMethod(sourceTypePropertyInfo.PropertyType,targetTypePropertyInfo.PropertyType);
var targetValue=genericMethodInfo.Invoke(new Mapper(),new[]{sourcePropertyValue});
targetTypePropertyInfo.SetValue(targetInstance,targetValue);
}
}
}
}
返回目标姿态;
}
}
称之为:
var student = Mapper.Convert<TblStudent, Student>(tblStudent);
var student=Mapper.Convert(tblStudent);
您可以这样做
public class Mapper<T, U> where U : new()
{
public U Convert(T input)
{
U newCastType = new U();
var fromObjectProperties = input.GetType().GetProperties();
var toObjectProperties = newCastType.GetType().GetProperties();
foreach (var parentProperty in fromObjectProperties)
{
foreach (var childProperty in toObjectProperties)
{
if((parentProperty.Name == childProperty.Name) && parentProperty.PropertyType.IsClass && parentProperty.PropertyType.Name != "String")
{
var typeInfo = typeof(Mapper<,>);
var genericType = typeInfo.MakeGenericType(parentProperty.PropertyType, childProperty.PropertyType);
var genericMethodInfo = genericType.GetMethod("Convert");
var ojb = Activator.CreateInstance(genericType);
var targetValue = genericMethodInfo.Invoke(ojb, new[] { parentProperty.GetValue(input) });
childProperty.SetValue(newCastType, targetValue);
}
else if ((parentProperty.Name == childProperty.Name))
{
childProperty.SetValue(newCastType, parentProperty.GetValue(input));
}
}
}
/*var fromObjectProperties = input.GetType().GetProperties();
foreach (var parentProperty in fromObjectProperties)
{
}*/
return newCastType;
}
}
公共类映射器,其中U:new()
{
公共U转换(T输入)
{
U newCastType=新U();
var fromObjectProperties=input.GetType().GetProperties();
var toObjectProperties=newCastType.GetType().GetProperties();
foreach(fromObjectProperties中的var parentProperty)
{
foreach(toObjectProperties中的var childProperty)
{
if((parentProperty.Name==childProperty.Name)&&parentProperty.PropertyType.IsClass&&parentProperty.PropertyType.Name!=“字符串”)
{
var typeInfo=typeof(映射器);
var genericType=typeInfo.MakeGenericType(parentProperty.PropertyType,childProperty.PropertyType);
var genericMethodInfo=genericType.GetMethod(“转换”);
var ojb=Activator.CreateInstance(genericType);
var targetValue=genericMethodInfo.Invoke(ojb,new[]{parentProperty.GetValue(input)});
SetValue(newCastType,targetValue);
}