C# Automatically assign property based on other property values

Viewed 50675

If I have some types e.g:

public class SomeType //Generated by some codegen i won't to change that.
{
    string C { get; set; }        
}

public class AnotherType : SomeType
{
    string A { get; set; }
    string B { get; set; }
}

Is it possible to assign property C automatically? For example when properties A and B get assigned or when I am Casting this type to some another type, or somehow else?

Basically for example I want to execute some logic to automatically assign property C according to values A and B at some point when properties values A and B get populated.

Is there any another ways of doing that rather than using standard properties?

I was thinking that it is possible to do some king of magic when i may cast type AnotherType to SomeType, but i cant implement implicit operator where i may put this conversion logic "from A+B to C" because compiler doesn't allow implicit operator for related types.

Now Only way i see it is remove inheritance and implement implicit operator for AnotherType to SomeType conversion, but the evil in that case i need to duplicate all properties of type SomeType within type AnotherType and i need to change type AnotherType manually every time when SomeType getting changed.

3 Answers
Related