Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I emit a .NET type with two properties that are overloaded only on return type?

I need to create a type that has two properties with the same name, and only differ on return type. Dynamically emitting this type via reflection is perfectly acceptable.

Something like this:

public TypeA Prop { get; }
public TypeB Prop { get; }

I do understand that I cannot consume this property from C# or VB.NET or lots of other .NET languages.

To prevent answers that explain to me why I don't want to do this, let me explain why I need it: I need it to reproduce a bug.

More specifically, I have a bug in AutoFixture where a Moq of a type will cause it to throw an exception in certain cases. The problem is that the type emitted by Moq contains two properties both named 'Mock' that differ only on the return type.

I would like to reproduce this scenario in a unit test, but I'd rather not take a dependency on Moq just for that single reason, so I'd like to reproduce the behavior internally in the test suite.

like image 692
Mark Seemann Avatar asked Oct 02 '09 11:10

Mark Seemann


People also ask

Can we achieve method overloading when two methods have only differences in return type?

Method overloading cannot be done by changing the return type of methods. The most important rule of method overloading is that two overloaded methods must have different parameters.

Can we overload method with different return type C#?

Overloading with same arguments and different return type −The return type doesn't matter. If they don't have different parameters, they both are still considered as same method and a compile time error will be generated.

Can overloaded functions have different return types?

The return type of a function has no effect on function overloading, therefore the same function signature with different return type will not be overloaded. Example: if there are two functions: int sum() and float sum(), these two will generate a compile-time error as function overloading is not possible here.

Why methods are not overloaded based on return type?

Overloading is the mechanism of binding the method call with the method body dynamically based on the parameters passed to the method call. ... It is not possible to decide to execute which method based on the return type, therefore, overloading is not possible just by changing the return type of the method.


1 Answers

You can have 2 properties with the same name that differ only by the type, and you can do that without dynamically emitting the type :

class Foo
{
    public string X
    {
        get { return "Hello world"; }
    }
}

class Bar : Foo
{
    public new int X
    {
        get { return 42; }
    }
}

void Main()
{
    foreach(PropertyInfo prop in typeof(Bar).GetProperties())
    {
        Console.WriteLine("{0} : {1}", prop.Name, prop.PropertyType);
    }
}

The output of this code will be :

X : System.Int32
X : System.String

like image 85
Thomas Levesque Avatar answered Nov 05 '22 05:11

Thomas Levesque