0

I have a class called CoverageBase with properties say A, B, C. And now based on some checks i need to append n number of properties say D and E properties in to this CoverageBase class along with my existing A, B, C. So for creating D and E dynamically during the runtime i am using Dictionary

class CoverageBase
{
   public string A { get; set; }
   public string B { get; set; }
   public string C { get; set; }
}

Dictionary<string, object> values = new Dictionary<string, object>()
{
  {"D",5},{"E",123}
};

var test = GetObject<CoverageBase>(values);

T GetObject<T>(Dictionary<string,object> dict)
{
  Type type = typeof(T);
  var obj = Activator.CreateInstance(type);

  foreach (var kv in dict)
  {
    type.GetProperty(kv.Key).SetValue(obj, kv.Value);
  }
  return (T)obj;
}

The problem with above approach is, when i try to GetProperty using "D" there will not be any property existing in that name in the CoverageBase class right. so it will throw error. So how do i build my expected class with properties A, B, C, D, E

Can we use any of the existing tools like ExpandoObject or DynamicObject for this?

9
  • 1
    You just can't add new properties to existing class. Commented Feb 19, 2018 at 6:40
  • 2
    @Evk can't we use any of the existing tools like ExpandoObject or DynamicObject for that? Commented Feb 19, 2018 at 6:42
  • Nope. Your CoverageBase has static set of 3 properties and there is no way to add or remove anything from it at runtime. You can generate new class at runtime which inherits from CoverageBase and adds those properties, but that's about it. Commented Feb 19, 2018 at 6:45
  • 2
    As usual, it's better to describe why you need this, because there is very high chance that you actually don't and your problem can be solved in a better way. As for inheriting at runtime - that's not trivial but there are libraries for that, such as github.com/ekonbenefits/impromptu-interface (well it can implement interface at runtime but that's similar). Commented Feb 19, 2018 at 6:59
  • 1
    Then maybe just add property of type Dictionary<string, object> to base coverage class and store additional properties there? Commented Feb 19, 2018 at 7:27

1 Answer 1

0

The important thing to remember with C# and other .Net based languages is the difference between strongly typed classes and non-strongly typed classes. In practice, Dynamics (non-strongly typed) are generally considered the lazy mans approach to rapid development. It is not advised to use Dynamics as a catch all approach unless you really need to.

There are ways to accomplish what you are going for, however, it would end up having to use dynamics at run time for when you want to interact with your class.

What you should look at is Newtonsoft.Json. Using Json, you can convert a C# Class into a JObject and then manipulate it as if it were a Dictionary. This will allow you to append extra fields and properties, but not methods. When you are finished modifying your object, you can convert it back to a dynamic and then interact with it as a normal expando object.

If you really want to create a strongly typed class at runtime, then you would need to look into runtime compilation. This would use the C# CodeDom provider and allows you to compile 'text' into C Sharp assemblies which contain one or more types. Those types can be anything that compiles successfully from your provided text input.

Sign up to request clarification or add additional context in comments.

Comments

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.