本文整理汇总了C#中System.Reflection.Emit.CodeGenerator.EndMethod方法的典型用法代码示例。如果您正苦于以下问题:C# CodeGenerator.EndMethod方法的具体用法?C# CodeGenerator.EndMethod怎么用?C# CodeGenerator.EndMethod使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.CodeGenerator
的用法示例。
在下文中一共展示了CodeGenerator.EndMethod方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateClassWriter
internal JsonFormatClassWriterDelegate GenerateClassWriter(ClassDataContract classContract)
{
_ilg = new CodeGenerator();
bool memberAccessFlag = classContract.RequiresMemberAccessForWrite(null, JsonGlobals.JsonSerializationPatterns);
try
{
BeginMethod(_ilg, "Write" + DataContract.SanitizeTypeName(classContract.StableName.Name) + "ToJson", typeof(JsonFormatClassWriterDelegate), memberAccessFlag);
}
catch (SecurityException securityException)
{
if (memberAccessFlag)
{
classContract.RequiresMemberAccessForWrite(securityException, JsonGlobals.JsonSerializationPatterns);
}
else
{
throw;
}
}
InitArgs(classContract.UnderlyingType);
_memberNamesArg = _ilg.GetArg(4);
WriteClass(classContract);
return (JsonFormatClassWriterDelegate)_ilg.EndMethod();
}
示例2: GenerateClassWriter
internal JsonFormatClassWriterDelegate GenerateClassWriter(ClassDataContract classContract)
{
ilg = new CodeGenerator();
bool memberAccessFlag = classContract.RequiresMemberAccessForWrite(null);
try
{
BeginMethod(ilg, "Write" + classContract.StableName.Name + "ToJson", typeof(JsonFormatClassWriterDelegate), memberAccessFlag);
}
catch (SecurityException securityException)
{
if (memberAccessFlag && securityException.PermissionType.Equals(typeof(ReflectionPermission)))
{
classContract.RequiresMemberAccessForWrite(securityException);
}
else
{
throw;
}
}
InitArgs(classContract.UnderlyingType);
memberNamesArg = ilg.GetArg(4);
DemandSerializationFormatterPermission(classContract);
DemandMemberAccessPermission(memberAccessFlag);
if (classContract.IsReadOnlyContract)
{
ThrowIfCannotSerializeReadOnlyTypes(classContract);
}
WriteClass(classContract);
return (JsonFormatClassWriterDelegate)ilg.EndMethod();
}
示例3: GenerateGetOnlyCollectionReader
public JsonFormatGetOnlyCollectionReaderDelegate GenerateGetOnlyCollectionReader(CollectionDataContract collectionContract)
{
ilg = GenerateCollectionReaderHelper(collectionContract, true /*isGetOnlyCollection*/);
ReadGetOnlyCollection(collectionContract);
return (JsonFormatGetOnlyCollectionReaderDelegate)ilg.EndMethod();
}
示例4: GetConvertHandler
public override ConvertHandler GetConvertHandler(Type inputType, Type outputType, object inputObject, object outputObject, Dictionary<string, string> mappingNames, List<string> ignoreList, Dictionary<int, string> mappingOrders, bool mappingSpecifiedOnly)
{
Dictionary<string, string> distToSrc = new Dictionary<string, string>();
Dictionary<string, MemberInfo> members = new Dictionary<string, MemberInfo>();
foreach (string sourceName in mappingNames.Keys)
{
MemberInfo sourceMember = ConvertorGeneratorHelper.GetMemberInfo(inputType, sourceName);
Check.Require(sourceMember != null, string.Format("member named {0} could not be found in {1}", sourceName, outputType.FullName));
distToSrc.Add(mappingNames[sourceName], sourceName);
members.Add(mappingNames[sourceName], sourceMember);
}
if (!mappingSpecifiedOnly)
{
Dictionary<string, MemberInfo> sourceMembers = ConvertorGeneratorHelper.GetMembers(inputType);
foreach (string sourceName in sourceMembers.Keys)
{
if (!ignoreList.Contains(sourceName) && !distToSrc.ContainsKey(sourceName))
{
distToSrc.Add(sourceName, sourceName);
members.Add(sourceName, sourceMembers[sourceName]);
}
}
}
CodeGenerator gen = new CodeGenerator();
gen.BeginMethod("m" + Guid.NewGuid().ToString("N"), typeof(ConvertHandler));
ArgBuilder inputObjectArg = new ArgBuilder(0, inputType);
ArgBuilder outputObjectArg = new ArgBuilder(1, outputType);
int currentCount = 0;
int memberCount = members.Count;
string[] keys = new string[memberCount];
distToSrc.Keys.CopyTo(keys, 0);
List<string> keyList = new List<string>(keys);
MemberSetterGenerator.BeginSetMembers(gen, outputObjectArg);
while (currentCount < memberCount)
{
currentCount++;
string targetName = ConvertorGeneratorHelper.GetCurrentKey(mappingOrders, currentCount, keyList).Replace("_", "");
if (string.IsNullOrEmpty(targetName))
{
continue;
}
string sourceName = distToSrc[targetName].Replace("_","");
if (MemberGetterGenerator.ContainsMember(sourceName, inputType, inputObject) && MemberSetterGenerator.ContainsMember(targetName, outputType, outputObject))
{
Type targetType = ConvertorGeneratorHelper.GetMemberType(members[targetName]);
LocalBuilder memberValue = gen.DeclareLocal(targetType, "memberValue");
MemberGetterGenerator.GetMemberValue(gen, inputObjectArg, sourceName, ref memberValue);
MemberSetterGenerator.SetMemberValue(gen, outputObjectArg, targetName, memberValue);
}
}
gen.Ldarg(outputObjectArg);
MemberSetterGenerator.EndSetMembers(gen, outputObjectArg);
return (ConvertHandler)gen.EndMethod();
}
示例5: GenerateClassReader
public JsonFormatClassReaderDelegate GenerateClassReader(ClassDataContract classContract)
{
ilg = new CodeGenerator();
bool memberAccessFlag = classContract.RequiresMemberAccessForRead(null);
try
{
BeginMethod(ilg, "Read" + classContract.StableName.Name + "FromJson", typeof(JsonFormatClassReaderDelegate), memberAccessFlag);
}
catch (SecurityException securityException)
{
if (memberAccessFlag && securityException.PermissionType.Equals(typeof(ReflectionPermission)))
{
classContract.RequiresMemberAccessForRead(securityException);
}
else
{
throw;
}
}
InitArgs();
DemandSerializationFormatterPermission(classContract);
DemandMemberAccessPermission(memberAccessFlag);
CreateObject(classContract);
ilg.Call(contextArg, XmlFormatGeneratorStatics.AddNewObjectMethod, objectLocal);
InvokeOnDeserializing(classContract);
if (classContract.IsISerializable)
ReadISerializable(classContract);
else
ReadClass(classContract);
if (Globals.TypeOfIDeserializationCallback.IsAssignableFrom(classContract.UnderlyingType))
ilg.Call(objectLocal, JsonFormatGeneratorStatics.OnDeserializationMethod, null);
InvokeOnDeserialized(classContract);
if (!InvokeFactoryMethod(classContract))
{
ilg.Load(objectLocal);
// Do a conversion back from DateTimeOffsetAdapter to DateTimeOffset after deserialization.
// DateTimeOffsetAdapter is used here for deserialization purposes to bypass the ISerializable implementation
// on DateTimeOffset; which does not work in partial trust.
if (classContract.UnderlyingType == Globals.TypeOfDateTimeOffsetAdapter)
{
ilg.ConvertValue(objectLocal.LocalType, Globals.TypeOfDateTimeOffsetAdapter);
ilg.Call(XmlFormatGeneratorStatics.GetDateTimeOffsetMethod);
ilg.ConvertValue(Globals.TypeOfDateTimeOffset, ilg.CurrentMethod.ReturnType);
}
else
{
ilg.ConvertValue(objectLocal.LocalType, ilg.CurrentMethod.ReturnType);
}
}
return (JsonFormatClassReaderDelegate)ilg.EndMethod();
}
示例6: GenerateCollectionReader
public JsonFormatCollectionReaderDelegate GenerateCollectionReader(CollectionDataContract collectionContract)
{
ilg = GenerateCollectionReaderHelper(collectionContract, false /*isGetOnlyCollection*/);
ReadCollection(collectionContract);
ilg.Load(objectLocal);
ilg.ConvertValue(objectLocal.LocalType, ilg.CurrentMethod.ReturnType);
return (JsonFormatCollectionReaderDelegate)ilg.EndMethod();
}
示例7: GenerateClassReader
public JsonFormatClassReaderDelegate GenerateClassReader(ClassDataContract classContract)
{
_ilg = new CodeGenerator();
bool memberAccessFlag = classContract.RequiresMemberAccessForRead(null);
try
{
BeginMethod(_ilg, "Read" + DataContract.SanitizeTypeName(classContract.StableName.Name) + "FromJson", typeof(JsonFormatClassReaderDelegate), memberAccessFlag);
}
catch (SecurityException securityException)
{
if (memberAccessFlag)
{
classContract.RequiresMemberAccessForRead(securityException);
}
else
{
throw;
}
}
InitArgs();
CreateObject(classContract);
_ilg.Call(_contextArg, XmlFormatGeneratorStatics.AddNewObjectMethod, _objectLocal);
InvokeOnDeserializing(classContract);
if (classContract.IsISerializable)
ReadISerializable(classContract);
else
ReadClass(classContract);
if (Globals.TypeOfIDeserializationCallback.IsAssignableFrom(classContract.UnderlyingType))
{
_ilg.Call(_objectLocal, JsonFormatGeneratorStatics.OnDeserializationMethod, null);
}
InvokeOnDeserialized(classContract);
if (!InvokeFactoryMethod(classContract))
{
_ilg.Load(_objectLocal);
// Do a conversion back from DateTimeOffsetAdapter to DateTimeOffset after deserialization.
// DateTimeOffsetAdapter is used here for deserialization purposes to bypass the ISerializable implementation
// on DateTimeOffset; which does not work in partial trust.
if (classContract.UnderlyingType == Globals.TypeOfDateTimeOffsetAdapter)
{
_ilg.ConvertValue(_objectLocal.LocalType, Globals.TypeOfDateTimeOffsetAdapter);
_ilg.Call(XmlFormatGeneratorStatics.GetDateTimeOffsetMethod);
_ilg.ConvertValue(Globals.TypeOfDateTimeOffset, _ilg.CurrentMethod.ReturnType);
}
//Copy the KeyValuePairAdapter<K,T> to a KeyValuePair<K,T>.
else if (classContract.IsKeyValuePairAdapter)
{
_ilg.Call(classContract.GetKeyValuePairMethodInfo);
_ilg.ConvertValue(Globals.TypeOfKeyValuePair.MakeGenericType(classContract.KeyValuePairGenericArguments), _ilg.CurrentMethod.ReturnType);
}
else
{
_ilg.ConvertValue(_objectLocal.LocalType, _ilg.CurrentMethod.ReturnType);
}
}
return (JsonFormatClassReaderDelegate)_ilg.EndMethod();
}
示例8: GenerateCollectionWriter
internal JsonFormatCollectionWriterDelegate GenerateCollectionWriter(CollectionDataContract collectionContract)
{
_ilg = new CodeGenerator();
bool memberAccessFlag = collectionContract.RequiresMemberAccessForWrite(null);
try
{
BeginMethod(_ilg, "Write" + DataContract.SanitizeTypeName(collectionContract.StableName.Name) + "ToJson", typeof(JsonFormatCollectionWriterDelegate), memberAccessFlag);
}
catch (SecurityException securityException)
{
if (memberAccessFlag)
{
collectionContract.RequiresMemberAccessForWrite(securityException);
}
else
{
throw;
}
}
InitArgs(collectionContract.UnderlyingType);
WriteCollection(collectionContract);
return (JsonFormatCollectionWriterDelegate)_ilg.EndMethod();
}
示例9: DoGetStaticMethodDelegate
/// <summary>
/// Do GetStaticMethodDelegate
/// </summary>
/// <param name="targetModule"></param>
/// <param name="genericMethodInfo"></param>
/// <param name="genericParameterTypes"></param>
/// <returns></returns>
protected static StaticDynamicMethodProxyHandler DoGetStaticMethodDelegate(
Module targetModule,
MethodInfo genericMethodInfo,
params Type[] genericParameterTypes)
{
#region Check preconditions
Check.Require(targetModule, "targetModule");
Check.Require(genericMethodInfo, "genericMethodInfo");
Check.Require((genericParameterTypes == null && genericMethodInfo.GetGenericArguments().Length == 0) ||
genericParameterTypes.Length == genericMethodInfo.GetGenericArguments().Length,
"The number of generic type parameter of genericMethodInfo and the input types must equal!");
Check.Require(genericMethodInfo.IsStatic, "genericMethodInfo must be static here!");
#endregion
//Create a dynamic method proxy delegate used to call the specified methodinfo
CodeGenerator gen = new CodeGenerator(targetModule);
gen.BeginMethod("dm" + Guid.NewGuid().ToString("N"), typeof(StaticDynamicMethodProxyHandler));
MethodInfo makeGenericMethodInfo = MakeMethodGeneric(genericMethodInfo, genericParameterTypes);
LoadParameters(gen, makeGenericMethodInfo.GetParameters(), true);
gen.Call(makeGenericMethodInfo);
CastValueToObject(gen, makeGenericMethodInfo.ReturnType);
return (StaticDynamicMethodProxyHandler)gen.EndMethod();
}
示例10: DoGetStaticFieldSetDelegate
/// <summary>
/// Do Get Static Field Set Delegate
/// </summary>
/// <param name="targetModule"></param>
/// <param name="fieldInfo"></param>
/// <returns></returns>
protected static StaticDynamicMethodProxyHandler DoGetStaticFieldSetDelegate(
Module targetModule,
FieldInfo fieldInfo
)
{
Check.Require(targetModule, "targetModule");
Check.Require(fieldInfo, "fieldInfo");
Check.Require(fieldInfo.IsStatic, "fieldInfo MUST be static here.");
CodeGenerator gen = new CodeGenerator(targetModule);
gen.BeginMethod("dm" + Guid.NewGuid().ToString("N"), typeof(StaticDynamicMethodProxyHandler));
gen.Ldarg(0);
gen.Ldc(0);
gen.Ldelem(typeof(object));
if (fieldInfo.FieldType.IsValueType)
gen.UnboxAny(fieldInfo.FieldType);
gen.StoreMember(fieldInfo);
gen.Load(null);
return (StaticDynamicMethodProxyHandler)gen.EndMethod();
}
示例11: DoGetStaticFieldGetDelegate
/// <summary>
/// Do Get Static Field Get Delegate
/// </summary>
/// <param name="targetModule"></param>
/// <param name="fieldInfo"></param>
/// <returns></returns>
protected static StaticDynamicMethodProxyHandler DoGetStaticFieldGetDelegate(
Module targetModule,
FieldInfo fieldInfo
)
{
Check.Require(targetModule, "targetModule");
Check.Require(fieldInfo, "fieldInfo");
Check.Require(fieldInfo.IsStatic, "fieldInfo MUST be static here.");
CodeGenerator gen = new CodeGenerator(targetModule);
gen.BeginMethod("dm" + Guid.NewGuid().ToString("N"), typeof(StaticDynamicMethodProxyHandler));
gen.LoadMember(fieldInfo);
CastValueToObject(gen, fieldInfo.FieldType);
return (StaticDynamicMethodProxyHandler)gen.EndMethod();
}