Open GoogleCodeExporter opened 9 years ago
May I suggest the following code instead of the current implementation of the
GetConversion method?
private static bool HasCast(Type type, Type from, Type to, out MethodInfo op)
{
const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
MemberFilter filter = delegate(MemberInfo m, object criteria)
{
if (m.Name != "op_Implicit" && m.Name != "op_Explicit")
{
return false;
}
MethodInfo methodInfo = (MethodInfo)m;
if (methodInfo.ReturnType != to)
{
return false;
}
ParameterInfo[] paramTypes = methodInfo.GetParameters();
return paramTypes.Length == 1 && paramTypes[0].ParameterType == from;
};
MemberInfo[] found = type.FindMembers(MemberTypes.Method, flags, filter, null);
if (found.Length == 0)
{
op = null;
return false;
}
op = (MethodInfo)found[0];
return true;
}
public MethodInfo GetConversion(bool toTail)
{
Type to = toTail ? declaredType : forType;
Type from = toTail ? forType : declaredType;
MethodInfo op;
if (HasCast(declaredType, from, to, out op) || HasCast(forType, from, to, out op))
{
return op;
}
throw new InvalidOperationException("No suitable conversion operator found fopr surrogate: " +
forType.FullName + " / " + declaredType.FullName);
}
Original comment by mark.kha...@gmail.com
on 21 Jul 2011 at 12:07
yes, that looks better; I've made an intermediate commit including a patch for
this - similar logic to the above, but slightly different code. Thanks for the
input.
Original comment by marc.gravell
on 21 Jul 2011 at 1:02
Original issue reported on code.google.com by
mark.kha...@gmail.com
on 21 Jul 2011 at 11:44