gpt4 book ai didi

c# - 找不到属性设置方法

转载 作者:行者123 更新时间:2023-11-30 17:49:24 28 4
gpt4 key购买 nike

当此行 bckPk = Translate(packs); 执行时,我收到了Property set method not found. 错误,这是很自然的。但是有人可以建议我解决这个问题,通过它我可以实现我在这里想要做的事情吗?

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Reflection;

namespace ExperimentProjects
{
public class ClassOne
{
public string PropertyOne { get; set; }
public string PropertyTwo { get; set; }
public string PropertyThree { get; set; }
}
public class ClassTwo
{
public string PropertyOne { get; set; }
public string PropertyTwo { get; set; }
public string PropertyThree { get; set; }

}

public class ClassPack : Collection<ClassOne>
{

}

public class ClassBckPack : Collection<ClassOne>
{

}
public class TranslateClass
{
public static TResult Translate<TSource, TResult>(TSource sourceObj)
{

Type typeOfSourceObj = sourceObj.GetType();
Type typeOfResultObj = typeof(TResult);
if (typeOfSourceObj.BaseType.Equals(typeOfResultObj.BaseType))
{
//Console.WriteLine("1");

}

ConstructorInfo constructorOfresultObj = typeOfResultObj.GetConstructor(System.Type.EmptyTypes);
Object[] parameters = new Object[0];
TResult result = (TResult)constructorOfresultObj.Invoke(parameters);
PropertyInfo[] propertiesInSourceObj = typeOfSourceObj.GetProperties();
if (propertiesInSourceObj.Length != 0)
{
foreach (PropertyInfo property in propertiesInSourceObj)
{
Console.WriteLine(property.PropertyType.Name);
PropertyInfo propertyOfResultObj = typeOfResultObj.GetProperty(property.Name);

if (propertyOfResultObj != null)
{
propertyOfResultObj.SetValue(result, property.GetValue(sourceObj));
}
}
}

Console.Read();
return result;
}
static void Main(string[] args)
{
ClassOne objOne = new ClassOne();
objOne.PropertyOne = "One";
objOne.PropertyTwo = "Two";
objOne.PropertyThree = "Three";
ClassTwo objTwo = Translate<ClassOne, ClassTwo>(objOne);
Console.WriteLine(objTwo.PropertyOne + " " + objTwo.PropertyTwo + " " + objTwo.PropertyThree);
ClassOne o = Translate<ClassOne, ClassOne>(objOne);
Console.WriteLine(o.PropertyOne + " " + o.PropertyTwo + " " + o.PropertyThree);
ClassPack packs = new ClassPack();
packs.Add(o);
packs.Add(objOne);

ClassBckPack bckPk = null;
try
{
bckPk = Translate<ClassPack, ClassBckPack>(packs);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.Read();
}
foreach (ClassOne eachObj in bckPk)
Console.WriteLine(eachObj.PropertyOne + " " + eachObj.PropertyTwo + " " + eachObj.PropertyThree);
Console.Read();
}
}
}

编辑:这里我想使用反射将对象从包复制到 bckPk,而不是使用 foreach 循环。例如,以下面的例子为例:

Class Content
{

}

Class AContents : Collection<Content>
{
}

Class BContents : Collection<Content>
{
}

Class BusinessEntity
{
public AContents
{
get; set;
}
}
Class DataContract
{
public AContents
{
get; set;
}
}

I want to use this Translate method this way now :

BusinessEntity be= new BusinessEntity ();
DataContract dc= new DataContract ();

dc=Translate<BusinessEntity,DataContract>(be);

如果我运行此代码,则会抛出未找到属性集方法错误

最佳答案

您收到异常是因为您试图为 ReadOnly 属性设置一个值。即,仅使用 getter 定义的属性。

这里你忘记了继承,因为你的类继承自基础 Collection<T>你得到异常的类

当您尝试读取 ClassPack 中的所有属性时并设置为 ClassBckPack , 他们继承了名为 Count 的成员这是只读的,这意味着它没有定义 Set方法。所以你得到了上面的异常。

请阅读以下API System.Collections.ObjectModel

 public class Collection<T> : IList<T>, ICollection<T>, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
{
// Summary:
// Initializes a new instance of the System.Collections.ObjectModel.Collection<T>
// class that is empty.
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public Collection();
//
// Summary:
// Initializes a new instance of the System.Collections.ObjectModel.Collection<T>
// class as a wrapper for the specified list.
//
// Parameters:
// list:
// The list that is wrapped by the new collection.
//
// Exceptions:
// System.ArgumentNullException:
// list is null.
public Collection(IList<T> list);

// Summary:
// Gets the number of elements actually contained in the System.Collections.ObjectModel.Collection<T>.
//
// Returns:
// The number of elements actually contained in the ****System.Collections.ObjectModel.Collection<T>.
public int Count { get; }****
//
// Summary:
// Gets a System.Collections.Generic.IList<T> wrapper around the System.Collections.ObjectModel.Collection<T>.
//
// Returns:
// A System.Collections.Generic.IList<T> wrapper around the System.Collections.ObjectModel.Collection<T>.
protected IList<T> Items { get; }

所以这里是解决方法

创建您的 CustomAttribute 说“ExampleAttribute”并仅应用于您尝试从源类更新到目标类的那些属性。然后读取所有属性检查属性是否是新属性的类型。这就是您将基类的属性与您的子类区分开来的方式。

 foreach (PropertyInfo propertyInfo in type.GetProperties())
{
object[] attrObjs = propertyInfo.GetCustomAttributes(typeof(ExampleAttribute), true);
if (attrObjs.Length > 0)
{
}
}

我觉得这是有道理的。谢谢

关于c# - 找不到属性设置方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21721673/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com