开发者

Implicit cast operator from object

开发者 https://www.devze.com 2023-01-29 08:23 出处:网络
I was wondering if someone could think of a nice workaround for not being able to add an implicit cast operator, for object, o开发者_StackOverflown your own class. The following example illustrates th

I was wondering if someone could think of a nice workaround for not being able to add an implicit cast operator, for object, o开发者_StackOverflown your own class. The following example illustrates the kind 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 won't compile because it upsets the C# compiler and it tells me

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

Turning response into Response and doing

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

is unfortunately not an option. My guess is going to be no, but could anyone think of a nice workaround/hack to get this to work. I would love to be able to do

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

and end up with a Response.Contents that said Whatever.Namespace.Bar


I'm not a fan of implicit conversions like this - other answers have explained why this isn't allowed. One option to make the conversion easy if you really don't want constructor calls in your code is via an extension method:

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

Then you can at least have:

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

Generally I'm wary of extension methods on object, mind you... does it really make sense to convert any object at all into a Response?


Think about

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

having the user-defined conversion defined for any of the base classes of Response. What shall happen in the example? The second assignment is ambiguous:

  • will r2 reference to a new Response instance with Content set to r.ToString() or
  • will r2 reference to the instance ris referring to?

(okay or will R2 have to do anything with some force, light sabers and stuff)

We can even expand our thoughts to subclasses of Response that are certainly allowed and correct because of polymorphy.

I guess there is no way to accomplish this. Moreover your code perhaps would be nicer to look at, but it would be far away from being well understandable.


I suggest using a the dynamic type instead of implicit object casting. It's basically what dynamic is, seemingly.

Moreover, see this question for the debate on conversions to or from a base class: User-defined conversion operator from base class However, this was a very specific case where the cast was really needed. You might need to rethink your design instead.


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

In your case, could you simply define a Bar -> Response operator?

When you perform type conversion, you must be able to define a conversion algorithm someway. Can you tell me how are you supposed to convert an object, the most generic type in .NET that has only a hash code and a string representation, into an object of a specific type?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号