本文整理汇总了C#中System.Reflection.Emit.CodeGenerator.Box方法的典型用法代码示例。如果您正苦于以下问题:C# CodeGenerator.Box方法的具体用法?C# CodeGenerator.Box怎么用?C# CodeGenerator.Box使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.CodeGenerator
的用法示例。
在下文中一共展示了CodeGenerator.Box方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetMemberValue
public void SetMemberValue(CodeGenerator generator, ArgBuilder targetObject, string memberName, LocalBuilder memberValue)
{
generator.Ldarg(targetObject);
generator.Load(memberName);
generator.Ldloc(memberValue);
// Type originalType = memberValue.LocalType.IsEnum ? typeof(int) : memberValue.LocalType;
if (memberValue.LocalType.IsValueType)
{
generator.Box(memberValue.LocalType.IsEnum ? typeof(int) : memberValue.LocalType);
}
//���ֵΪ�գ����ܵ���ToString
generator.Load(null);
generator.If(Cmp.NotEqualTo);
generator.Ldloc(memberValue);
// Type originalType = memberValue.LocalType.IsEnum ? typeof(int) : memberValue.LocalType;
if (memberValue.LocalType.IsValueType)
{
generator.Box(memberValue.LocalType.IsEnum ? typeof(int) : memberValue.LocalType);
}
generator.Call(typeof(object).GetMethod("ToString", Type.EmptyTypes));
generator.Else();
generator.LoadDefaultValue(memberValue.LocalType);
if (memberValue.LocalType.IsValueType)
{
generator.Box(memberValue.LocalType.IsEnum ? typeof(int) : memberValue.LocalType);
}
generator.EndIf();
generator.Call(typeof(NameValueCollection).GetMethod("Add", new Type[] { typeof(string), typeof(string) }));
}
示例2: SetMemberValue
public void SetMemberValue(CodeGenerator generator, ArgBuilder targetObject, string memberName, LocalBuilder memberValue)
{
generator.Ldarg(targetObject);
generator.Load(memberName);
generator.Ldloc(memberValue);
if (memberValue.LocalType.IsValueType)
{
generator.Box(memberValue.LocalType.IsEnum ? typeof(int) : memberValue.LocalType);
}
generator.Call(typeof(IDictionary).GetMethod("Add"));
}
示例3: GetMemberValue
public void GetMemberValue(CodeGenerator generator, ArgBuilder targetObject, string memberName, ref LocalBuilder memberValue)
{
Type inputType = targetObject.ArgType;
MemberInfo memberInfo = ConvertorGeneratorHelper.GetMemberInfo(inputType, memberName);
if (memberInfo == null)
{
return;
}
Type srcMemberType = ConvertorGeneratorHelper.GetMemberType(memberInfo);
Type destMemberType = memberValue.LocalType;
if (memberValue.LocalType.IsAssignableFrom(srcMemberType))
{
generator.Ldarg(targetObject);
generator.LoadMember(memberInfo);
generator.Stloc(memberValue);
return;
}
Type converTargetType = ConvertorGeneratorHelper.GetOriginalType(destMemberType);
Type convertSourceType = ConvertorGeneratorHelper.GetOriginalType(srcMemberType);
//ת������
if (typeof(IConvertible).IsAssignableFrom(convertSourceType))
{
generator.Ldarg(targetObject);
generator.LoadMember(memberInfo);
if (convertSourceType.IsValueType)
{
generator.Box(srcMemberType);
}
//�Ƿ�Ϊ��
generator.Load(null);
generator.If(Cmp.NotEqualTo);
//�����Ϊ�գ���������ת��
generator.Ldarg(targetObject);
generator.LoadMember(memberInfo);
if (convertSourceType.IsValueType)
{
generator.Box(srcMemberType);
}
generator.Load(converTargetType);
generator.Call(typeof(Convert).GetMethod("ChangeType", new Type[] { typeof(object), typeof(Type) }));
if (destMemberType.IsValueType)
{
generator.UnboxAny(destMemberType);
}
generator.Stloc(memberValue);
//���Ϊ�գ������Ĭ��ֵ
generator.Else();
generator.LoadDefaultValue(destMemberType);
generator.Stloc(memberValue);
generator.EndIf();
}
}
示例4: LoadParameters
private static void LoadParameters(CodeGenerator gen, ParameterInfo[] pis, bool isMethodStatic)
{
Check.Require(gen, "gen");
if (pis != null)
{
for (int i = 0; i < pis.Length; ++i)
{
if (isMethodStatic)
{
gen.Ldarg(0);
}
else
{
gen.Ldarg(1);
}
gen.Ldc(i);
Type srcType = pis[i].ParameterType;
string str = srcType.ToString();
if (str.EndsWith("&"))
{
srcType = CommonUtils.GetType(str.Substring(0, str.Length - 1));
}
if (str.EndsWith("&")) //ref or out param
{
if (srcType.IsValueType && (pis[i].Attributes & ParameterAttributes.Out) != ParameterAttributes.Out) //ref value param
{
gen.Ldelem(typeof(object));
gen.Unbox(srcType);
}
else
{
if (srcType.IsValueType && srcType != typeof(object)) //out value param
{
gen.LoadDefaultValue(srcType);
gen.Box(srcType);
gen.Stelem(typeof(object));
if (isMethodStatic)
{
gen.Ldarg(0);
}
else
{
gen.Ldarg(1);
}
gen.Ldc(i);
gen.Ldelem(typeof(object));
gen.Unbox(srcType);
}
else //ref or out class param
{
gen.Ldelema(typeof(object));
}
}
}
else
{
gen.Ldelem(typeof(object));
if (srcType.IsValueType)
{
gen.UnboxAny(srcType);
}
else if (srcType != typeof(object))
{
gen.Castclass(srcType);
}
}
}
}
}
示例5: CastValueToObject
private static void CastValueToObject(CodeGenerator gen, Type valueType)
{
if (valueType == typeof(void))
{
gen.Load(null);
}
else if (valueType.IsValueType)
{
gen.Box(valueType);
}
else if (valueType != typeof(object))
{
gen.Castclass(typeof(object));
}
}
示例6: SetMemberValue
public void SetMemberValue(CodeGenerator generator, ArgBuilder targetObject, string memberName, LocalBuilder memberValue)
{
generator.Ldarg(targetObject);
if (memberValue.LocalType.FullName.StartsWith("System.Nullable`1["))
{
generator.Ldloc(memberValue);
generator.Load(null);
generator.If(Cmp.EqualTo);
generator.LoadMember(typeof(DBNull).GetField("Value"));
generator.Stloc(memberValue);
generator.EndIf();
}
generator.Load(null);
generator.If(Cmp.EqualTo);
PropertyInfo columns = typeof(DataTable).GetProperty("Columns");
generator.Ldloc(locDataTable);
generator.LoadMember(columns);
generator.Load(memberName);
generator.Ldtoken(CommonUtils.GetOriginalTypeOfNullableType(memberValue.LocalType.IsEnum ? typeof(int) : memberValue.LocalType));
generator.Call(typeof(Type).GetMethod("GetTypeFromHandle", new Type[] { typeof(RuntimeTypeHandle) }));
generator.Call(typeof(DataColumnCollection).GetMethod("Add", new Type[] { typeof(string), typeof(Type) }));
generator.Pop();
generator.Ldloc(locMemberValues);
generator.Load(memberName);
generator.Ldloc(memberValue);
if (memberValue.LocalType.IsValueType)
{
generator.Box(memberValue.LocalType.IsEnum ? typeof(int) : memberValue.LocalType);
}
generator.Call(typeof(Dictionary<string, object>).GetMethod("Add"));
generator.Else();
generator.Ldarg(targetObject);
generator.Load(memberName);
generator.Ldloc(memberValue);
if (memberValue.LocalType.IsValueType)
{
generator.Box(memberValue.LocalType);
}
generator.Call(typeof(DataRow).GetMethod("set_Item", new Type[] { typeof(string), typeof(object) }));
generator.EndIf();
}