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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With