0

I have some class with "complex" objects like Enumerators, Lists and others. I need to construct the same anonymous object from instance of this class. However, I need to convert all enumerators, bools, Lists and some others types in a way I want.

public class MyClass {
   public string Str1 {get;set;}
   public bool B1 {get;set;}
   public MyEnum EnumVal {get;set;} 
}

So the instance of this

var cl = new MyClass() { Str1 = "Some rand", B1 = true, EnumVal = MyEnum.Value1 } ;

should be converted into some anonymous class with values:

{ Str1 = "Some rand", B1 = 1, EnumVal = 1 } 

I thought about having two classes like MyClass and MySimplifiedClass and use AutoMapper, but in future MyClass could be extended and I do not want manually add properties to MySimplifiedClass. Instead I have defined rules how some of types should be converted (these are booleans, enumerators, Lists).

3
  • And why exactly do you want to do this? Commented Jun 26, 2023 at 18:12
  • I have to save this class into specific file format. The client who reads this file has these specific rules already defined. So they do no tread bool as true/false, but they need 1 or 0. They have their own rules how Lists/Arrays should look like. There are already existing methods to create this specific file format, I just need to pass there my class. If I pass MyClass instance it will fail. I need to serialize to another class or anonymous class that fits the rules. Commented Jun 27, 2023 at 7:29
  • What format exactly? Why can't you just save serialize from original data applying the rules without using intermediate anonymous objects? Are those methods extensible? Can you show some minimal reproducible example? Usually fiddling with dynamic type creation is the worst option. Commented Jun 27, 2023 at 7:31

0

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.