0

I would like to know if there is a way to create a custom class out of an existing class in this manner:

original class:

public class Person { string name; } 

later in code:

var diffPerson = CreateDiffClass<Person>(); diffPerson.name.Value = "name"; diffPerson.name.Changed = false; 

this diffPerson is not of Person type, instead it is custom created one, that for every variable in Person have one in itself so that the new variable is a tuple where T is the type of the variable.

I want it to create a system for comparing 2 instances of the same class. one old instance and one new and save the new value and if it changed

I don't really know how to describe it except showing in this example so I hope it is understandable..

I want this to be generic and work on any given class

Thanks

11
  • 1
    The example doesn't make any sense. The code in later in code won't even compile. Value or Changed, none of them are members of string. According to your code, they even contain different values. Are you talking about class inheritance? Maybe you are talking about an interface or so.. ? Commented Jun 26, 2019 at 15:39
  • 1
    It's a bit unclear what problem you're solving. Why do you need this? Commented Jun 26, 2019 at 15:41
  • the diffPerson variable is not Person type Commented Jun 26, 2019 at 15:42
  • 1
    It sounds like you're basically saying you want the ability to compare 2 classes of the same type, irrespective of the actual class, is that right? Commented Jun 26, 2019 at 15:48
  • I think I understand what you are asking for. Yes, it can be done. No it isn't fast. If you want to query and read/write all the public properties of an unknown class then you need to look at the "reflection" classes (System.Reflection) Commented Jun 26, 2019 at 15:56

1 Answer 1

0

You can declare a generic class like this

public class CustomValue<T> { public T Value { get; set; } public bool Changed { get; set; } } 

and then use it like this

public class Person { public CustomValue<string> Name; } 

later in code

var diffPerson = new Person(); diffPerson.Name = new CustomValue<string>(); diffPerson.Name.Value = "name"; diffPerson.Name.Changed = false; 
Sign up to request clarification or add additional context in comments.

1 Comment

I want to do this in code, not by creating the new Person type hardcoded but by having a way to do this generically for every class for every variable

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.