Skip to content

Base class properties are not take into account for model generation #14

@mahdighorbanpour

Description

@mahdighorbanpour

If we define abstract base class to hold some repeated properties for a family of related classes, these properties are not taken into account for generating the derived class model definition. Consider this example

 public abstract class Space : TwinBase
    {
        [TwinProperty]
        public string Name { get; set; }
    }

    [DigitalTwin(Version = 1, DisplayName = "Floor")]
    public class Floor : Space
    {
        [TwinProperty]
        public string Level { get; set; }
    }

    [DigitalTwin(Version = 1, DisplayName = "Room")]
    public class Room : Space
    {
        [TwinProperty]
        public string Number { get; set; }
    }

This will produce this model:

{
  "@id": "dtmi:domain:floor;1",
  "@type": "Interface",
  "@context": "dtmi:dtdl:context;2",
  "displayName": "Floor",
  "contents": [
    {
      "@type": "Property",
      "name": "level",
      "schema": "string"
    }
  ]
}

{
  "@id": "dtmi:domain:room;1",
  "@type": "Interface",
  "@context": "dtmi:dtdl:context;2",
  "displayName": "Room",
  "contents": [
    {
      "@type": "Property",
      "name": "number",
      "schema": "string"
    }
  ]
}

The Name property is missing. If I decorate the base class with DigitalTwin attribute, still I don't get the shared properties in the output, however the model has a new part which says it is extending another model (the base class model). But then there is a new problem, when I try to create the model for the base class, I get this error:

System.MissingMethodException : Cannot create an abstract class.

  Stack Trace: 
RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean wrapExceptions, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& hasNoDefaultCtor)
RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache)
RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions)
Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
Activator.CreateInstance(Type type)
DigitalTwinSerializer.SerializeModel(Type twinType, Boolean htmlEncode, JsonSerializerOptions serializerOptions) line 119
TwinSerialization_Tests.ShouldSerialiseModelToDTDL(Type type) line 39

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions