none
Getting Dynamic Properties RRS feed

  • Question

  • I am trying to solve this GitHub issue:

    https://github.com/GregFinzer/Compare-Net-Objects/issues/103

    Currently the Compare .NET Objects library only supports Expando Dynamic Objects.  I am trying to add the ability to compare regular dynamic objects.

    Given this code:

    public class Entity
    {
        public Guid Id { get; set; }
        public String Name { get; set; }
    }
    
    Delta<Entity> x = new Delta<Entity>();
    dynamic dynamicX = x;
    dynamicX.Name = nameof(Entity);
    dynamicX.Id = typeof(Entity).GUID;

    I am not able to get the Name or Id from GetProperties:

    x.GetType().GetProperties


    Is there some way to get those properties?

    Sunday, July 1, 2018 11:44 AM

Answers

  • A dynamic object is simply an object that implements the IDynamicMetaObjectProvider interface. Everything in it is effectively a dictionary but the actual implementation is undefined. In C# the dynamic keyword maps to the DynamicObject type I believe. This type has a method called GetDynamicMemberNames you could try using. 

    You didn't provide a definition for Delta<T> so it is hard to say what results you expect. It wouldn't have Id or Name though (presumably) because that is coming from a generic type parameter, not Delta itself. Most likely Delta<T> would have a property of type T that exposed the entity for which you could get the child properties. In your case, since it is a dynamic object, then your setters would be adding those "properties" to the root object.

    Note that your dynamicX isn't actually a dynamic. Yes you assigned a value to it but it isn't of type DynamicObject so trying to "add" members to it like you are won't work. If Delta were itself a dynamic object then things might be easier.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, July 2, 2018 2:40 PM
    Moderator