You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Somewhat of an edge case perhaps, but we just got a bug report over at Moq that essentially boils down to this:
using Castle.DynamicProxy;publicclassFoo{publicFoo(Aa){}publicFoo(Bb){}}publicclassA{}publicclassB{}vargenerator=new ProxyGenerator();varproxy=(Foo)generator.CreateClassProxy(typeof(Foo),newobject[]{default(A)},new StandardInterceptor());
which throws the following exception:
System.Reflection.AmbiguousMatchException: Ambiguous match found.
at System.DefaultBinder.BindToMethod(BindingFlags bindingAttr, MethodBase[] match, Object[]& args, ParameterModifier[] modifiers, CultureInfo cultureInfo, String[] names, Object& state)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxyInstance(Type proxyType, List`1 proxyArguments, Type classToProxy, Object[] constructorArguments)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Object[] constructorArguments, IInterceptor[] interceptors)
at Program.Main()
The reason is quite clear, the ctor argument default(A) is equivalent to null, and that null is being put in a object[] array, so all type information is lost and it's impossible for Reflection to decide whether Foo..ctor(A) or Foo..ctor(B) was intended to be called.
The only way around this problem would be to enhance DynamicProxy's API such that either (a) the targeted base ctor could be specified in the form of a ConstructorInfo; or (b) the ctor argument types could get passed in a separate Type[].
The text was updated successfully, but these errors were encountered:
Why not have a special "marker class" to carry two facts combined: 1) the value is null and 2) type is that type.?
For example, if we have class like this:
So ProxyGenerator can choose correct constructor either by using "hardcoded NullValueOf class", or in a more flexible way, going through user defined conversion operators.
Somewhat of an edge case perhaps, but we just got a bug report over at Moq that essentially boils down to this:
which throws the following exception:
The reason is quite clear, the ctor argument
default(A)
is equivalent tonull
, and thatnull
is being put in aobject[]
array, so all type information is lost and it's impossible for Reflection to decide whetherFoo..ctor(A)
orFoo..ctor(B)
was intended to be called.The only way around this problem would be to enhance DynamicProxy's API such that either (a) the targeted base ctor could be specified in the form of a
ConstructorInfo
; or (b) the ctor argument types could get passed in a separateType[]
.The text was updated successfully, but these errors were encountered: