public MethodInfo DefineMethod(string name, Type returnType, Type[] paramTypes, string[] paramNames, XmlILMethodAttributes xmlAttrs)
{
MethodInfo methResult;
int uniqueId = 1;
string nameOrig = name;
Type[] paramTypesNew;
bool isRaw = (xmlAttrs & XmlILMethodAttributes.Raw) != 0;
// Ensure that name is unique
while (_methods[name] != null)
{
// Add unique id to end of name in order to make it unique within this module
uniqueId++;
name = nameOrig + " (" + uniqueId + ")";
}
if (!isRaw)
{
// XmlQueryRuntime is always 0th parameter
paramTypesNew = new Type[paramTypes.Length + 1];
paramTypesNew[0] = typeof(XmlQueryRuntime);
Array.Copy(paramTypes, 0, paramTypesNew, 1, paramTypes.Length);
paramTypes = paramTypesNew;
}
if (!_useLRE)
{
MethodBuilder methBldr;
methBldr = _typeBldr.DefineMethod(
name,
MethodAttributes.Private | MethodAttributes.Static,
returnType,
paramTypes);
if (_emitSymbols && (xmlAttrs & XmlILMethodAttributes.NonUser) != 0)
{
// Add DebuggerStepThroughAttribute and DebuggerNonUserCodeAttribute to non-user methods so that debugging is a better experience
methBldr.SetCustomAttribute(new CustomAttributeBuilder(XmlILConstructors.StepThrough, new object[] { }));
methBldr.SetCustomAttribute(new CustomAttributeBuilder(XmlILConstructors.NonUserCode, new object[] { }));
}
if (!isRaw)
methBldr.DefineParameter(1, ParameterAttributes.None, RuntimeName);
for (int i = 0; i < paramNames.Length; i++)
{
if (paramNames[i] != null && paramNames[i].Length != 0)
methBldr.DefineParameter(i + (isRaw ? 1 : 2), ParameterAttributes.None, paramNames[i]);
}
methResult = methBldr;
}
else
{
DynamicMethod methDyn = new DynamicMethod(name, returnType, paramTypes, s_LREModule);
methDyn.InitLocals = true;
methResult = methDyn;
}
// Index method by name
_methods[name] = methResult;
return methResult;
}