views:

70

answers:

1

Hi,

I have a container class for adding some properties to standard data types like int, string and so on. This container class encapsulate an object of such an (standard type) object. Other classes then use sub classes of the container class for getting/setting the added properties. Now I want that the sub classes can implicitly cast between its encapsulated objects and itself without having extra code in it.

Here is a simplified example of my classes:

  // Container class that encapsulates strings and adds property ToBeChecked
  // and should define the cast operators for sub classes, too.
  internal class StringContainer
  {
    protected string _str;

    public bool ToBeChecked { get; set; }

    public static implicit operator StringContainer(string str)
    {
      return new StringContainer { _str = str };
    }

    public static implicit operator string(StringContainer container)
    {
      return (container != null) ? container._str : null;
    }
  }

  // An example of many sub classes. Its code should be as short as possible.
  internal class SubClass : StringContainer
  {
    // I want to avoid following cast operator definition
    //    public static implicit operator SubClass(string obj)
    //    {
    //      return new SubClass() { _str = obj };
    //    }
  }

  // Short code to demosntrate the usings of the implicit casts.
  internal class MainClass
  {
    private static void Main(string[] args)
    {
      SubClass subClass = "test string"; // ERROR: Cannot convert source type 'string' to 'SubClass'

      string testString = subClass; // No error
    }
  }

My real container class has two type parameters, one for the type of encapsulated object (string, int,...) and a second for the sub class type (e.g. SubClass).

How can I make the code

SubClass subClass = "test string"; // ERROR: Cannot convert source type 'string' to 'SubClass'

runnable by minimal code in sub classes?

+1  A: 

I don't think there is a way to define conversion operator in the base class.

The base class doesn't know anything about the sub class, so it doesn't have enough information to construct it. For example, what if your SubClass type had only a constructor that requires some arguments? The base class doesn't know about the sub class, so it cannot construct it in any way.

Maybe you could use another way to parameterize the StringContainer type. For example, instead of using implementation inheritance (sub classes), you could instead pass some functions (delegates of type Func<...>) to the StringContainer class. This way, the user could parameterize the class and your implicit conversion would still work.

Tomas Petricek
@base class doesn't know anything about the sub class: I could set a type parameter to the subclass type. E.g. internal class StringContainer<SubClass>
Markus
@Markus: Theoretically, using a type parameter would be a solution, but I don't think that C# will allow you to write `public static implicit operator T(..)` where `T` is a type parameter.
Tomas Petricek
Thank you Tomas. I think I will use the work around to not using subclasses.
Markus