【问题标题】:Property set method not found未找到属性设置方法
【发布时间】:2014-03-10 09:49:00
【问题描述】:

当这行 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);

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

【问题讨论】:

  • 那么你到底想做什么?在这种情况下,它试图设置 Count 属性...您可以轻松判断属性是否可写(使用 CanWrite 属性),但我认为这对您没有太大帮助。
  • 这只是我的印象,但是对泛型方法使用那么多反射感觉不对。在某些情况下,这当然是必要的。
  • 请看编辑后的帖子

标签: c# reflection collections


【解决方案1】:

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

这里你忘了继承,因为你的类是从基类继承的 Collection&lt;T&gt; 类你得到了异常

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

请从System.Collections.ObjectModel阅读以下API

 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)
            {
             }
         }

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

【讨论】:

  • 是的,我明白了。现在你能建议我一种使翻译发生的方法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-30
  • 1970-01-01
  • 1970-01-01
  • 2019-10-06
  • 1970-01-01
相关资源
最近更新 更多