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]'.

7
  • 1
    Reflection solution will be much more complex and extra-work, can i suggest another approach, like Serialization or it a must ? Commented Dec 19, 2017 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? Commented Dec 19, 2017 at 23:13
  • See Copy the property values to another object with C# and also 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<List<Namespace.Property>>(); Commented Dec 19, 2017 at 23:17
  • Serialization is not an option, becuase the JsonProperty attribute. @dbc they have similar properties (they are differetn just because its namespaces) Commented Dec 19, 2017 at 23:26
  • Need to see a minimal reproducible example. But maybe this would help: How to ignore JsonProperty(PropertyName = “someName”) when serializing json?. Commented Dec 19, 2017 at 23:31

1 Answer 1

-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);
Sign up to request clarification or add additional context in comments.

3 Comments

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.
Added a solution that force ignore on JsonPropertyAttribute.
I have no idea why this answer gets downvoted, I would appreciate if someone will write explaining comment why their choose to do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.