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
c #
TheCodeJunkie
source share