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

123456
  • 113
  • 8
  • 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.. ? – Software Dev Jun 26 '19 at 15:39
  • 1
    It's a bit unclear what problem you're solving. Why do you need this? – Julian Jun 26 '19 at 15:41
  • the diffPerson variable is not Person type – 123456 Jun 26 '19 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? – sr28 Jun 26 '19 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) – Steve Todd Jun 26 '19 at 15:56
  • I know some about reflection but I have no idea how to get to something like this. reflection lets me read the properties, types and such. but how do i do this? – 123456 Jun 26 '19 at 16:01
  • sr28- yes but i also want to save it in a "special" way. if you have another idea ill be glad to hear – 123456 Jun 26 '19 at 16:09
  • That sounds cryptic. What do you mean by a 'special' way and what bearing does that have on the comparison you're trying to make? – sr28 Jun 26 '19 at 16:11
  • Like I said I want to save the resulting comparison in a way that will be easy for me to use later. In the example above it is in a customly created class that for every variable has a tuple with the recent value and true / false if it changed in the comparison – 123456 Jun 26 '19 at 16:17
  • I believe [duplicate](https://stackoverflow.com/questions/2502395/comparing-object-properties-using-reflection) is exactly what you *should be looking for* (as I can't see any reasonable usage for `CreateDiffClass` you are asking for here). If you are looking for something else - some clarification of why you need that class could help. – Alexei Levenkov Jun 26 '19 at 16:17
  • Some good explanation how you plan to use that "class that for every variable has a `tuple` " would help. I don't see sensible way to write code with such class as you'd not know list of properties... (saying in other way - I don't see *any benefits* over `Dictionary>` mapping properties to that tuple) – Alexei Levenkov Jun 26 '19 at 16:21

1 Answers1

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;
zeeshan
  • 56
  • 7
  • 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 – 123456 Jun 26 '19 at 15:48