0

Both object are "the same" but its imposible for me, to copy de object definition directly.

The class A looks like:

public class A { 
    [JsonProperty(PropertyName="MyPROP")]
    List<Namespace.Property> pro {get; set;}
}

The class B looks like:

public class B { 
    [JsonProperty(PropertyName="MyNewPropertyName")]
    List<MyNames.Property> pro {get; set;}
}

As you can see, the only differences are attributes and namespace, both classes has the same methods and properties.

The error I'm getting using reflection is this

El objeto de tipo 'System.Collections.Generic.List1[Namespace.Property]' no puede convertirse en el tipo 'System.Collections.Generic.List1[MyNames.Property]'.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Mario Mixtega
  • 96
  • 1
  • 3
  • 1
    Reflection solution will be much more complex and extra-work, can i suggest another approach, like Serialization or it a must ? – Orel Eraki Dec 19 '17 at 23:12
  • 2
    `Namespace.Property` and `MyNames.Property` are different types. Why do you think you should be able to convert one to another? Do they have similar properties? – dbc Dec 19 '17 at 23:13
  • See [Copy the property values to another object with C#](https://stackoverflow.com/q/3445784) and also [tag:automapper]. But since you are using Json.NET apparently, a quick approach might be to do a serialization round-trip like so: `a.pro = JToken.FromObject(b.pro).ToObject>();` – dbc Dec 19 '17 at 23:17
  • Serialization is not an option, becuase the JsonProperty attribute. @dbc they have similar properties (they are differetn just because its namespaces) – Mario Mixtega Dec 19 '17 at 23:26
  • Need to see a [mcve]. But maybe this would help: [How to ignore JsonProperty(PropertyName = “someName”) when serializing json?](https://stackoverflow.com/q/20622492). – dbc Dec 19 '17 at 23:31
  • @dbc can you give an example of how to make it with automapper please? :D – Mario Mixtega Dec 19 '17 at 23:31
  • C# doesn't use duck-typing so just because the methods and properties are the same it doesn't make the classes compatible. You have to instantiate new objects to change the type of an object in C#. – Enigmativity Dec 19 '17 at 23:52

1 Answers1

-3

Reflection is a lot of extra work, you can achieve a solution without it. In case reflection isn't a must, here is a few options.

Serialization: Serializing source object and Deserializing into another, using StringSerialization(Json or Xml).

Here is Brian Rogers code on another question, that ignores JsonPropertyAttribute.

class LongNameContractResolver : DefaultContractResolver
{
    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        // Let the base class create all the JsonProperties 
        // using the short names
        IList<JsonProperty> list = base.CreateProperties(type, memberSerialization);

        // Now inspect each property and replace the 
        // short name with the real property name
        foreach (JsonProperty prop in list)
        {
            prop.PropertyName = prop.UnderlyingName;
        }

        return list;
    }
}

AutoMapper: Mapping the inner classes, and if your properties are the same, you can skip manually mapping the entire classes.

Complete Fiddle

Mapper.Initialize(cfg => cfg.CreateMap<A, B>().ReverseMap());

var objA = new A
{
    Prop = new List<AClass>
    {
        new AClass { Name = "Magneto" },
        new AClass { Name = "Wolverine" }
    }
};

var objB = Mapper.Map<B>(objA);
Console.WriteLine(objB.Prop[0].Name);
Console.WriteLine(objB.Prop[1].Name);
Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
  • 1
    Serialization does the trick, but avoiding the "PropertyName" attribute as @dbc said. Thanks Orel, and dbc, if some day I see you, I will invite the beers. – Mario Mixtega Dec 19 '17 at 23:55
  • Added a solution that force ignore on JsonPropertyAttribute. – Orel Eraki Dec 20 '17 at 00:12
  • I have no idea why this answer gets downvoted, I would appreciate if someone will write explaining comment why their choose to do so. – Orel Eraki Jul 17 '23 at 11:07