简体   繁体   中英

Provide implementation for implicit construction in base class

Is there a way, in a base class, to provide an implicit conversion that works for all inherited classes?

Here's a simplified example of a class I'm using:

public class Date1 {
   public Date1(DateTime value) {
       Value = value;
   }

   public DateTime Value { get; set; }

   public static implicit operator Date1(string value) {
      DateTime dt;
      DateTime.TryParse(value, out dt);
      return new Date1(dt);
   }

   public void SerializeTo(TextWriter textWriter)
   {
      textWriter.Write("{0:yyMM}", Value);
   }
}

I plan to make Date2 and Date3 classes whose only differences from Date1 are the names and the particular format specifier in SerializeTo . It seems like I should be able to put the implicit operator into a base class from which all these classes inherit. Is that possible? The problem is that in the base class, I have to return an instance of the correct subclass, and I don't know if that's even possible, or advisable.

Or is there a better way to construct these classes?

PS Please see my follow-up question where I request critique on a possible way to accomplish what I'm asking for. (I only thought of this some time after asking this question.)

@DavidArno already mentioned in a comment why it's not possible technically, but let's forget that for the moment and consider an example of what you are trying to achieve.

Let's say I have a method with the following signature: ConsumeDate1(Date1 date) . If you provide such "magical conversion" as you described, I should be able to pass a string to the method ( ConsumeDate1("1507") ), implicit conversion should take place and the result should be given to the method implementation. The only information the conversion has, is that the input is a string, and that output should be Date1 or a derived class . How would the conversion ever know which class to choose? The only one it knows about is Date1 . Even if you were able to redefine the conversion in Date2 and Date3 , the conversion operation couldn't magically decide, which implementation to convert to.

In cases like this, inheritance unfortunately doesn't help. You can for example provide static factory methods ( Parse , FromString ...) on each of the derived classes.

The problem with these obviously is that you have to explicitly choose which type to use - ConsumeDate1(Date3.Parse("1507")) , but we've already established that the conversion cannot figure that out itself and would need help anyway.

Another issue is that you cannot enforce those static method on all derived classes by C#'s type system. If you want to make sure they are implemented (at least in the code you own), you can use Convetion Testing and reflectively check that all subclasses of Date1 contain such static method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM