本文整理汇总了C#中MethodBase.Clone方法的典型用法代码示例。如果您正苦于以下问题:C# MethodBase.Clone方法的具体用法?C# MethodBase.Clone怎么用?C# MethodBase.Clone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MethodBase
的用法示例。
在下文中一共展示了MethodBase.Clone方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SelectMethod
// Given a set of methods that match the base criteria, select a method based
// upon an array of types. This method should return null if no method matchs
// the criteria.
public static MethodBase SelectMethod(MethodBase[] match, Type[] types)
{
int i;
int j;
MethodBase[] candidates = (MethodBase[])match.Clone();
// Find all the methods that can be described by the types parameter.
// Remove all of them that cannot.
int CurIdx = 0;
for (i = 0; i < candidates.Length; i++)
{
ParameterInfo[] par = candidates[i].GetParameters();
if (par.Length != types.Length)
continue;
for (j = 0; j < types.Length; j++)
{
Type pCls = par[j].ParameterType;
if (pCls == types[j])
continue;
if (pCls == typeof(Object))
continue;
if (pCls.GetTypeInfo().IsPrimitive)
{
if (!CanConvertPrimitive(types[j], pCls))
break;
}
else
{
if (!pCls.GetTypeInfo().IsAssignableFrom(types[j].GetTypeInfo()))
break;
}
}
if (j == types.Length)
candidates[CurIdx++] = candidates[i];
}
if (CurIdx == 0)
return null;
if (CurIdx == 1)
return candidates[0];
// Walk all of the methods looking the most specific method to invoke
int currentMin = 0;
bool ambig = false;
int[] paramOrder = new int[types.Length];
for (i = 0; i < types.Length; i++)
paramOrder[i] = i;
for (i = 1; i < CurIdx; i++)
{
int newMin = FindMostSpecificMethod(candidates[currentMin], paramOrder, null, candidates[i], paramOrder, null, types, null);
if (newMin == 0)
ambig = true;
else
{
if (newMin == 2)
{
currentMin = i;
ambig = false;
currentMin = i;
}
}
}
if (ambig)
throw new AmbiguousMatchException(SR.Arg_AmbiguousMatchException);
return candidates[currentMin];
}
示例2: SelectMethod
// Given a set of methods that match the base criteria, select a method based
// upon an array of types. This method should return null if no method matchs
// the criteria.
public sealed override MethodBase SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers)
{
int i;
int j;
Type[] realTypes = new Type[types.Length];
for (i = 0; i < types.Length; i++)
{
realTypes[i] = types[i].UnderlyingSystemType;
if (!(realTypes[i].IsRuntimeImplemented()))
throw new ArgumentException(SR.Arg_MustBeType, nameof(types));
}
types = realTypes;
// We don't automatically jump out on exact match.
if (match == null || match.Length == 0)
throw new ArgumentException(SR.Arg_EmptyArray, nameof(match));
MethodBase[] candidates = (MethodBase[])match.Clone();
// Find all the methods that can be described by the types parameter.
// Remove all of them that cannot.
int CurIdx = 0;
for (i = 0; i < candidates.Length; i++)
{
ParameterInfo[] par = candidates[i].GetParametersNoCopy();
if (par.Length != types.Length)
continue;
for (j = 0; j < types.Length; j++)
{
Type pCls = par[j].ParameterType;
if (pCls == types[j])
continue;
if (pCls == CommonRuntimeTypes.Object)
continue;
if (pCls.IsPrimitive)
{
if (!(types[j].UnderlyingSystemType.IsRuntimeImplemented()) ||
!CanConvertPrimitive(types[j].UnderlyingSystemType, pCls.UnderlyingSystemType))
break;
}
else
{
if (!pCls.IsAssignableFrom(types[j]))
break;
}
}
if (j == types.Length)
candidates[CurIdx++] = candidates[i];
}
if (CurIdx == 0)
return null;
if (CurIdx == 1)
return candidates[0];
// Walk all of the methods looking the most specific method to invoke
int currentMin = 0;
bool ambig = false;
int[] paramOrder = new int[types.Length];
for (i = 0; i < types.Length; i++)
paramOrder[i] = i;
for (i = 1; i < CurIdx; i++)
{
int newMin = FindMostSpecificMethod(candidates[currentMin], paramOrder, null, candidates[i], paramOrder, null, types, null);
if (newMin == 0)
ambig = true;
else
{
if (newMin == 2)
{
currentMin = i;
ambig = false;
currentMin = i;
}
}
}
if (ambig)
throw new AmbiguousMatchException(SR.Arg_AmbiguousMatchException);
return candidates[currentMin];
}
示例3: BindToMethod
// This method is passed a set of methods and must choose the best
// fit. The methods all have the same number of arguments and the object
// array args. On exit, this method will choice the best fit method
// and coerce the args to match that method. By match, we mean all primitive
// arguments are exact matchs and all object arguments are exact or subclasses
// of the target. If the target OR is an interface, the object must implement
// that interface. There are a couple of exceptions
// thrown when a method cannot be returned. If no method matchs the args and
// ArgumentException is thrown. If multiple methods match the args then
// an AmbiguousMatchException is thrown.
//
// The most specific match will be selected.
//
public static MethodBase BindToMethod(MethodBase[] match, ref Object[] args)
{
if (match == null || match.Length == 0)
throw new ArgumentException(SR.Arg_EmptyArray, "match");
Contract.EndContractBlock();
MethodBase[] candidates = (MethodBase[])match.Clone();
int i;
int j;
#region Map named parameters to candidate parameter postions
// We are creating an paramOrder array to act as a mapping
// between the order of the args and the actual order of the
// parameters in the method. This order may differ because
// named parameters (names) may change the order. If names
// is not provided, then we assume the default mapping (0,1,...)
int[][] paramOrder = new int[candidates.Length][];
for (i = 0; i < candidates.Length; i++)
{
ParameterInfo[] par = candidates[i].GetParameters();
// args.Length + 1 takes into account the possibility of a last paramArray that can be omitted
paramOrder[i] = new int[(par.Length > args.Length) ? par.Length : args.Length];
// Default mapping
for (j = 0; j < args.Length; j++)
paramOrder[i][j] = j;
}
#endregion
Type[] paramArrayTypes = new Type[candidates.Length];
Type[] argTypes = new Type[args.Length];
#region Cache the type of the provided arguments
// object that contain a null are treated as if they were typeless (but match either object
// references or value classes). We mark this condition by placing a null in the argTypes array.
for (i = 0; i < args.Length; i++)
{
if (args[i] != null)
{
argTypes[i] = args[i].GetType();
}
}
#endregion
// Find the method that matches...
int CurIdx = 0;
Type paramArrayType = null;
#region Filter methods by parameter count and type
for (i = 0; i < candidates.Length; i++)
{
paramArrayType = null;
// If we have named parameters then we may have a hole in the candidates array.
if (candidates[i] == null)
continue;
// Validate the parameters.
ParameterInfo[] par = candidates[i].GetParameters();
#region Match method by parameter count
if (par.Length == 0)
{
#region No formal parameters
if (args.Length != 0)
{
if ((candidates[i].CallingConvention & CallingConventions.VarArgs) == 0)
continue;
}
// This is a valid routine so we move it up the candidates list.
paramOrder[CurIdx] = paramOrder[i];
candidates[CurIdx++] = candidates[i];
continue;
#endregion
}
else if (par.Length > args.Length)
{
#region Shortage of provided parameters
// If the number of parameters is greater than the number of args then
//.........这里部分代码省略.........
示例4: BindToMethod
// This method is passed a set of methods and must choose the best
// fit. The methods all have the same number of arguments and the object
// array args. On exit, this method will choice the best fit method
// and coerce the args to match that method. By match, we mean all primitive
// arguments are exact matchs and all object arguments are exact or subclasses
// of the target. If the target OR is an interface, the object must implement
// that interface. There are a couple of exceptions
// thrown when a method cannot be returned. If no method matchs the args and
// ArgumentException is thrown. If multiple methods match the args then
// an AmbiguousMatchException is thrown.
//
// The most specific match will be selected.
//
public sealed override MethodBase BindToMethod(
BindingFlags bindingAttr, MethodBase[] match, ref Object[] args,
ParameterModifier[] modifiers, CultureInfo cultureInfo, String[] names, out Object state)
{
if (match == null || match.Length == 0)
throw new ArgumentException(SR.Arg_EmptyArray, nameof(match));
MethodBase[] candidates = (MethodBase[])match.Clone();
int i;
int j;
state = null;
#region Map named parameters to candidate parameter postions
// We are creating an paramOrder array to act as a mapping
// between the order of the args and the actual order of the
// parameters in the method. This order may differ because
// named parameters (names) may change the order. If names
// is not provided, then we assume the default mapping (0,1,...)
int[][] paramOrder = new int[candidates.Length][];
for (i = 0; i < candidates.Length; i++)
{
ParameterInfo[] par = candidates[i].GetParametersNoCopy();
// args.Length + 1 takes into account the possibility of a last paramArray that can be omitted
paramOrder[i] = new int[(par.Length > args.Length) ? par.Length : args.Length];
if (names == null)
{
// Default mapping
for (j = 0; j < args.Length; j++)
paramOrder[i][j] = j;
}
else
{
// Named parameters, reorder the mapping. If CreateParamOrder fails, it means that the method
// doesn't have a name that matchs one of the named parameters so we don't consider it any further.
if (!CreateParamOrder(paramOrder[i], par, names))
candidates[i] = null;
}
}
#endregion
Type[] paramArrayTypes = new Type[candidates.Length];
Type[] argTypes = new Type[args.Length];
#region Cache the type of the provided arguments
// object that contain a null are treated as if they were typeless (but match either object
// references or value classes). We mark this condition by placing a null in the argTypes array.
for (i = 0; i < args.Length; i++)
{
if (args[i] != null)
{
argTypes[i] = args[i].GetType();
}
}
#endregion
// Find the method that matches...
int CurIdx = 0;
bool defaultValueBinding = ((bindingAttr & BindingFlags.OptionalParamBinding) != 0);
Type paramArrayType = null;
#region Filter methods by parameter count and type
for (i = 0; i < candidates.Length; i++)
{
paramArrayType = null;
// If we have named parameters then we may have a hole in the candidates array.
if (candidates[i] == null)
continue;
// Validate the parameters.
ParameterInfo[] par = candidates[i].GetParametersNoCopy();
#region Match method by parameter count
if (par.Length == 0)
{
#region No formal parameters
if (args.Length != 0)
{
if ((candidates[i].CallingConvention & CallingConventions.VarArgs) == 0)
//.........这里部分代码省略.........