本文整理汇总了C#中System.Reflection.Emit.ILGenerator.EmitLoadType方法的典型用法代码示例。如果您正苦于以下问题:C# ILGenerator.EmitLoadType方法的具体用法?C# ILGenerator.EmitLoadType怎么用?C# ILGenerator.EmitLoadType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.ILGenerator
的用法示例。
在下文中一共展示了ILGenerator.EmitLoadType方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EmitConvertAndSetValue
/// <summary>
/// Emit the IL to convert the current value on the stack and set the value of the object.
/// </summary>
/// <param name="il">The IL generator to output to.</param>
/// <param name="sourceType">The current type of the value.</param>
/// <param name="mapping">The column mapping to use.</param>
/// <remarks>
/// Expects the stack to contain:
/// Target Object
/// Value to set
/// The value is first converted to the type required by the method parameter, then sets the property.
/// </remarks>
/// <returns>A label that needs to be marked at the end of a succesful set.</returns>
public static Label EmitConvertAndSetValue(ILGenerator il, Type sourceType, ColumnMappingEventArgs mapping)
{
var method = mapping.ClassPropInfo;
// targetType - the target type we need to convert to
// underlyingTargetType - if the target type is nullable, we need to look at the underlying target type
// rawTargetType - if the underlying target type is enum, we need to look at the underlying target type for that
// sourceType - this is the type of the data in the data set
Type targetType = method.MemberType;
Type underlyingTargetType = Nullable.GetUnderlyingType(targetType) ?? targetType;
// some labels that we need
Label isDbNullLabel = il.DefineLabel();
Label finishLabel = il.DefineLabel();
// if the value is DbNull, then we continue to the next item
il.Emit(OpCodes.Dup); // dup value, stack => [target][value][value]
il.Emit(OpCodes.Isinst, typeof(DBNull)); // isinst DBNull:value, stack => [target][value-as-object][DBNull or null]
il.Emit(OpCodes.Brtrue_S, isDbNullLabel); // br.true isDBNull, stack => [target][value-as-object]
// handle the special target types first
if (targetType == typeof(char))
{
// char
il.EmitCall(OpCodes.Call, _readChar, null);
}
else if (targetType == typeof(char?))
{
// char?
il.EmitCall(OpCodes.Call, _readNullableChar, null);
}
else if (targetType == TypeHelper.LinqBinaryType)
{
// unbox sql byte arrays to Linq.Binary
// before: stack => [target][object-value]
// after: stack => [target][byte-array-value]
il.Emit(OpCodes.Unbox_Any, typeof(byte[])); // stack is now [target][byte-array]
// before: stack => [target][byte-array-value]
// after: stack => [target][Linq.Binary-value]
il.Emit(OpCodes.Newobj, TypeHelper.LinqBinaryCtor);
}
else if (targetType == typeof(XmlDocument))
{
// special handler for XmlDocuments
// before: stack => [target][object-value]
il.Emit(OpCodes.Call, _readXmlDocument);
// after: stack => [target][xmlDocument]
}
else if (targetType == typeof(XDocument))
{
// special handler for XDocuments
// before: stack => [target][object-value]
il.Emit(OpCodes.Call, _readXDocument);
// after: stack => [target][xDocument]
}
else if (sourceType == typeof(string) && CanDeserialize(mapping.Serializer, targetType))
{
// we are getting a string from the database, but the target is not a string, and it's a reference type
// assume the column is a serialized data type and that we want to deserialize it
// before: stack => [target][object-value]
il.Emit(OpCodes.Castclass, typeof(string));
il.EmitLoadType(targetType);
// after: stack => [target][object-value][memberType]
// determine the serializer to use to convert the string to an object
var serializerMethod = mapping.Serializer.GetMethod("Deserialize", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(string), typeof(Type) }, null);
if (serializerMethod == null)
throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Serializer type {0} needs the method 'public static object Deserialize(string, Type)'", mapping.Serializer.Name));
il.Emit(OpCodes.Call, serializerMethod);
il.Emit(OpCodes.Unbox_Any, targetType);
}
else if (underlyingTargetType.IsEnum && sourceType == typeof(string))
{
var localString = il.DeclareLocal(typeof(string));
// if we are converting a string to an enum, then parse it.
// see if the value from the database is a string. if so, we need to parse it. If not, we will just try to unbox it.
il.Emit(OpCodes.Isinst, typeof(string)); // is string, stack => [target][string]
il.Emit(OpCodes.Stloc, localString); // pop loc.2 (enum), stack => [target]
//.........这里部分代码省略.........
示例2: EmitConvertValue
/// <summary>
/// Emits the IL to convert a value to a target type.
/// </summary>
/// <param name="il">The IL generator to output to.</param>
/// <param name="memberName">The name of the member being converted.</param>
/// <param name="sourceType">The source type.</param>
/// <param name="targetType">The target type.</param>
/// <param name="serializer">The serializer to use to deserialize the value.</param>
public static void EmitConvertValue(ILGenerator il, string memberName, Type sourceType, Type targetType, IDbObjectSerializer serializer)
{
// targetType - the target type we need to convert to
// underlyingTargetType - if the target type is nullable, we need to look at the underlying target type
// rawTargetType - if the underlying target type is enum, we need to look at the underlying target type for that
// sourceType - this is the type of the data in the data set
Type underlyingTargetType = Nullable.GetUnderlyingType(targetType) ?? targetType;
// some labels that we need
var finishLabel = il.DefineLabel();
Label isDbNullLabel = il.DefineLabel();
// if the value is DbNull, then we continue to the next item
il.Emit(OpCodes.Dup); // dup value, stack => [target][value][value]
il.Emit(OpCodes.Isinst, typeof(DBNull)); // isinst DBNull:value, stack => [target][value-as-object][DBNull or null]
il.Emit(OpCodes.Brtrue_S, isDbNullLabel); // br.true isDBNull, stack => [target][value-as-object]
// handle the special target types first
if (targetType == typeof(char))
{
// char
il.EmitCall(OpCodes.Call, _readChar, null);
}
else if (targetType == typeof(char?))
{
// char?
il.EmitCall(OpCodes.Call, _readNullableChar, null);
}
else if (targetType == TypeHelper.LinqBinaryType)
{
// unbox sql byte arrays to Linq.Binary
// before: stack => [target][object-value]
// after: stack => [target][byte-array-value]
il.Emit(OpCodes.Unbox_Any, typeof(byte[])); // stack is now [target][byte-array]
// before: stack => [target][byte-array-value]
// after: stack => [target][Linq.Binary-value]
il.Emit(OpCodes.Newobj, TypeHelper.LinqBinaryCtor);
}
else if (targetType == typeof(XmlDocument))
{
// special handler for XmlDocuments
// before: stack => [target][object-value]
il.Emit(OpCodes.Call, _readXmlDocument);
// after: stack => [target][xmlDocument]
}
else if (targetType == typeof(XDocument))
{
// special handler for XDocuments
// before: stack => [target][object-value]
il.Emit(OpCodes.Call, _readXDocument);
// after: stack => [target][xDocument]
}
else if (serializer != null && serializer.CanDeserialize(sourceType, targetType))
{
// we are getting a string from the database, but the target is not a string, and it's a reference type
// assume the column is a serialized data type and that we want to deserialize it
il.EmitLoadType(targetType);
StaticFieldStorage.EmitLoad(il, serializer);
il.Emit(OpCodes.Call, typeof(TypeConverterGenerator).GetMethod("DeserializeObject", BindingFlags.NonPublic | BindingFlags.Static));
il.Emit(OpCodes.Unbox_Any, targetType);
}
else if (underlyingTargetType.IsEnum && sourceType == typeof(string))
{
var localString = il.DeclareLocal(typeof(string));
// if we are converting a string to an enum, then parse it.
// see if the value from the database is a string. if so, we need to parse it. If not, we will just try to unbox it.
il.Emit(OpCodes.Isinst, typeof(string)); // is string, stack => [target][string]
il.Emit(OpCodes.Stloc, localString); // pop loc.2 (enum), stack => [target]
// call enum.parse (type, value, true)
il.EmitLoadType(underlyingTargetType);
il.Emit(OpCodes.Ldloc, localString); // push enum, stack => [target][enum-type][string]
il.Emit(OpCodes.Ldc_I4_1); // push true, stack => [target][enum-type][string][true]
il.EmitCall(OpCodes.Call, _enumParse, null); // call Enum.Parse, stack => [target][enum-as-object]
// Enum.Parse returns an object, which we need to unbox to the enum value
il.Emit(OpCodes.Unbox_Any, underlyingTargetType);
}
else if (EmitConstructorConversion(il, sourceType, targetType))
{
// target type can be constructed from source type
}
else
{
// this isn't a system value type, so unbox to the type the reader is giving us (this is a system type, hopefully)
// now we have an unboxed sourceType
//.........这里部分代码省略.........