Implicit casting operator from object

I was wondering if anyone could think of a good workaround so that it would not be possible to add an implicit casting operator, for an object, to its own class. The following example illustrates the type of code I would like

public class Response { public string Contents { get; set; } public static implicit operator Response(object source) { return new Response { Contents = source.ToString(); }; } } 

This does not compile because it upsets the C # compiler and it tells me

 user-defined conversions to or from a base class are not allowed 

Including response in response and execution

 public static implicit operator Response<T>(T source) 

Unfortunately, not an option. I think this will not be the case, but someone might think of a good workaround / hack to make this work. I would like to be able to do

 public Response Foo() { return new Bar(); } 

and ended up with Response.Contents, which said anything .Namespace.Bar

+7
c #
source share
4 answers

Think about

 Response r = CreateSomeExampleResponse(); Response r2 = r; 

has a user-defined transformation defined for any of the base Response classes. What will happen in this example? The second task is ambiguous:

  • there will be an r2 link to a new Response instance with Content set to r.ToString() or
  • will r2 reference an instance of r refer to?

(well, or R2 will do something with some power, light sabers, etc.)

We can even expand our thoughts to subclasses of Response , which are certainly valid and correct due to polymorphism.

I think this is not to be done. Moreover, your code may be nicer to watch, but it will be far from fully understood.

+4
source share

I'm not a fan of implicit conversions like this - other answers explained why this is not allowed. One option to make the conversion easier is if you really do not want the constructor calls in your code to be received using the extension method:

 public static ResponseExtensions { public static Response ToResponse(this object source) { return new Response { Contents = source.ToString(); }; } } 

Then you can at least:

 public Response Foo() { return new Bar().ToResponse(); } 

As a rule, I fear extension methods on object , of course ... does it really make sense to convert any object in general to Response ?

+4
source share

I suggest using a dynamic type instead of implicit casting of objects. This is, in fact, what kind of dynamics, apparently.

Also, see this question for a discussion of conversions to or from a base class: Custom conversion operator from a base class However, this was a very specific case when a throw is really needed. You may need to rethink your design.

+2
source share

there is no solution to your problem with your cats. This is clearly indicated in the error: user-defined conversions to or from a base class are not allowed . Since each class inherits object , you have your own answer. Inheriting from Response to object means that all instances of Response are also instances of object !!! The implicit cast from object to Response means that every object is a Response , which is nonsense !!!

In your case, could you simply define the operator Bar → Response ?

When you do type conversion , you must somehow determine the conversion algorithm. Can you tell me how you should convert object , the most common type in .NET, which has only a hash code and a string representation, into an object of a certain type?

+1
source share

All Articles