C# 如何设置targetPi?我认为这个定义不正确,但我不理解;I don’我不知道该怎么解决这个问题?

C# 如何设置targetPi?我认为这个定义不正确,但我不理解;I don’我不知道该怎么解决这个问题?,c#,reflection,settings,propertyinfo,C#,Reflection,Settings,Propertyinfo,//这里有一些代码 object target = Activator.CreateInstance(typeof(T)); PropertyInfo[] sourceProperties = sourceType.GetProperties(); foreach (PropertyInfo pi in sourceProperties) { PropertyInfo targetPi = typeof(T).GetProperty(p

//这里有一些代码

    object target = Activator.CreateInstance(typeof(T));   

    PropertyInfo[] sourceProperties = sourceType.GetProperties();

    foreach (PropertyInfo pi in sourceProperties)
    {
         PropertyInfo targetPi = typeof(T).GetProperty(pi.Name); //returns null why?

         object piValue = pi.GetValue(source, null);

         try
         {
               if (targetPi != null)   // it doesnt work 
               {
                    targetPi.SetValue(target,piValue, null); // target has typeof(T)
               }
         }
         catch { }         
    }
    return(T)target;
}
这对我很有用:

struct Item1 { public double Foo { get; set; } }
struct Item2 { public double Foo { get; set; } }

class Program
{
    static void Main(string[] args)
    {
        Item1 i1 = new Item1();
        CopyDynamically(new Item2 { Foo = 42 }, ref i1);

        Console.WriteLine(i1.Foo);


        i1 = CopyDynamically<Item1>(new Item2 { Foo = 3.14 });

        Console.WriteLine(i1.Foo);
    }

    static void CopyDynamically<T>(object source, ref T target)
    {
        if (source == null)
            throw new ArgumentNullException("source");
        if (target == null)
            throw new ArgumentNullException("target");

        foreach (PropertyInfo pi in source.GetType().GetProperties())
        {
            PropertyInfo targetPi = typeof(T).GetProperty(pi.Name);

            if (targetPi != null && targetPi.SetMethod != null && targetPi.SetMethod.IsPublic)
            {
                object val = pi.GetValue(source, null);
                object o = target;

                try { targetPi.SetValue(o, val, null); }
                catch { }

                target = (T)o;
            }
        }
    }
    static T CopyDynamically<T>(object source, params object[] ctorArgs)
    {
        T target = (T)Activator.CreateInstance(typeof(T), ctorArgs);

        CopyDynamically(source, ref target);

        return target;
    }
}
struct Item1{public double Foo{get;set;}
struct Item2{public double Foo{get;set;}}
班级计划
{
静态void Main(字符串[]参数)
{
Item1 i1=新的Item1();
动态复制(新项目2{Foo=42},参考i1);
Console.WriteLine(i1.Foo);
i1=动态复制(新项2{Foo=3.14});
Console.WriteLine(i1.Foo);
}
静态无效复制动态(对象源,引用T目标)
{
if(source==null)
抛出新的ArgumentNullException(“源”);
if(target==null)
抛出新的ArgumentNullException(“目标”);
foreach(source.GetType().GetProperties()中的PropertyInfo pi)
{
PropertyInfo targetPi=typeof(T).GetProperty(pi.Name);
if(targetPi!=null&&targetPi.SetMethod!=null&&targetPi.SetMethod.IsPublic)
{
object val=pi.GetValue(源,空);
对象o=目标;
请尝试{targetPi.SetValue(o,val,null);}
捕获{}
目标=(T)o;
}
}
}
静态复制动态复制(对象源,参数对象[]参数参数)
{
T target=(T)Activator.CreateInstance(typeof(T),ctorArgs);
动态复制(源、参考目标);
回报目标;
}
}

您还没有告诉我们您想要实现的目标。您刚刚发布了格式错误的代码,仅此而已。请看,实际上,我一直在尝试映射到表到表,我发现,错误是在我的代码背景上。(表变量名称错误)。谢谢你的帮助mtman。如果有问题,任何人都可以问我:)