Property set method not found.

When this line is bckPk = Translate (packages); is executed, I get a property set method not found. a mistake that is quite natural. But can anyone suggest me a workaround for this, with which I can achieve what I am trying to do here?

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();
        }
    }
}

EDIT: Here I want to copy an object from packages to bckPk using Reflection without using a foreach loop. For example, take the following example:

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);

If I run this code, it will throw a property set method, no error was found

+3
source share
2 answers

, ReadOnly. .. , .

, Collection<T>,

ClassPack ClassBckPack, Count, readonly, , 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)
            {
             }
         }

, .

+1

, AutoMap , , , : .

0

All Articles