コード例 #1
0
		void LoadParameterOrNull(ParameterInfo pi, Type type)
		{
			EmitHelper emit = Context.MethodBuilder.Emitter;
			object[] attrs = pi.GetCustomAttributes(typeof(ParamNullValueAttribute), true);

			object nullValue = attrs.Length == 0 ?
				null : ((ParamNullValueAttribute)attrs[0]).Value;

			Label labelNull = emit.DefineLabel();
			Label labelEndIf = emit.DefineLabel();

			if (pi.Attributes == ParameterAttributes.Out)
			{
				emit
					.ldnull
					.end()
					;

				return;
			}

			if (nullValue != null)
			{
				Type nullValueType = type;
				bool isNullable = TypeHelper.IsNullable(type);

				if (type.IsEnum)
				{
					nullValueType = Enum.GetUnderlyingType(type);
					nullValue = System.Convert.ChangeType(nullValue, nullValueType);
				}
				else if (isNullable)
				{
					nullValueType = type.GetGenericArguments()[0];

					emit
						.ldarga(pi)
						.call(type, "get_HasValue")
						.brfalse(labelNull)
						;
				}

				if (nullValueType == nullValue.GetType() && emit.LoadWellKnownValue(nullValue))
				{
					if (nullValueType == typeof(string))
						emit
							.ldargEx(pi, false)
							.call(nullValueType, "Equals", nullValueType)
							.brtrue(labelNull)
							;
					else if (isNullable)
						emit
							.ldarga(pi)
							.call(type, "get_Value")
							.beq(labelNull)
							;
					else
						emit
							.ldargEx(pi, false)
							.beq(labelNull)
						;
				}
				else
				{
					string nullString = TypeDescriptor.GetConverter(nullValue).ConvertToInvariantString(nullValue);
					FieldBuilder staticField = CreateNullValueField(nullValueType, nullString);
					MethodInfo miEquals = new TypeHelper(nullValueType).GetPublicMethod("Equals", nullValueType);

					if (miEquals == null)
					{
						// Is it possible?
						//
						throw new TypeBuilderException(string.Format(
							Resources.DataAccessorBuilder_EqualsMethodIsNotPublic, type.FullName));
					}

					if (isNullable)
						emit
							.ldsflda(staticField)
							.ldarga(pi)
							.call(pi.ParameterType, "get_Value")
							;
					else
						emit
							.ldsflda(staticField)
							.ldarg(pi)
						;

					if (miEquals.GetParameters()[0].ParameterType.IsClass)
						emit
							.boxIfValueType(nullValueType)
							;

					emit
						.call(miEquals)
						.brtrue(labelNull)
						;
				}
			}

			if (type.IsEnum)
				emit
					.ldloc(_locManager)
					.callvirt(typeof(DbManager).GetProperty("MappingSchema").GetGetMethod())
					;

			emit
				.ldargEx(pi, true)
				;

			if (type.IsEnum)
				emit
					.ldc_i4_1
					.callvirt(typeof(MappingSchema), "MapEnumToValue", typeof(object), typeof(bool))
					;

			if (nullValue != null)
			{
				emit
					.br(labelEndIf)
					.MarkLabel(labelNull)
					.ldnull
					.MarkLabel(labelEndIf)
					;
			}
		}