samedi 5 mars 2016

How do I track the context a type resides in?

I have a type that looks like this:

public class MyClass
{
    public ContextType Context;
    public int Value;
}

The Context is used to keep track of which context the type resides in. The problem is when an assignment is performed:

MyClass myClassA = ..
MyClass myClassB = ..

myClassA = myClassB;

The context gets copied from myClassB to myClassA when I only want the internal value to be copied.

I've tried using an implicit operator but it does not allow the conversion to be to or from a base type.

I want to avoid forcing the user to use some kind of custom assignment method but if I really have to I want to prevent regular assignment to be possible (to avoid what will be a common mistake). I my API to be as user-friendly as possible.

Is there a way to accomplish this behavior? Any suggestions on other ways to track context of a type?

Aucun commentaire:

Enregistrer un commentaire