Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit cast operator from object

Tags:

c#

I was wondering if someone could think of a nice workaround for not being able to add an implicit cast operator, for object, on 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

like image 681
TheCodeJunkie Avatar asked Dec 08 '10 23:12

TheCodeJunkie


People also ask

What is implicit operator in C#?

The Implicit Operator According to MSDN, an implicit keyword is used to declare an implicit user-defined type conversion operator. In other words, this gives the power to your C# class, which can accepts any reasonably convertible data type without type casting.

What is an implicit cast in C++?

The word implicit means understood or embedded. In implicit C++ type casting, the data type in which the value is to be converted is not specified in the program. It is automatically done by the C++ compiler.

Which operator can be used to convert in C#?

Use the implicit conversion operator in C# The following code snippet shows how you can take advantage of the implicit operator to convert an Author instance to an AuthorDto instance.


4 Answers

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?

like image 191
Jon Skeet Avatar answered Sep 20 '22 22:09

Jon Skeet


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.

like image 23
Andreas Avatar answered Sep 20 '22 22:09

Andreas


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.

like image 34
Lazlo Avatar answered Sep 24 '22 22:09

Lazlo


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?

like image 41
usr-local-ΕΨΗΕΛΩΝ Avatar answered Sep 23 '22 22:09

usr-local-ΕΨΗΕΛΩΝ