Exemplo n.º 1
0
		void AddParameter(ParameterInfo pi)
		{
			Type pType = pi.ParameterType;

			if (pType.IsByRef)
				pType = pType.GetElementType();

			if (TypeHelper.IsScalar(pType)
#if FW4
				|| pi.IsRefCursor()
#endif
				)
				_paramList.Add(pi);
			else if (pType == typeof(DbManager) || pType.IsSubclassOf(typeof(DbManager)))
				_createManager = false;
			else
				_refParamList.Add(pi);
		}
Exemplo n.º 2
0
		void StoreParameterValue(LocalBuilder param, ParameterInfo pi, Type type)
		{
			EmitHelper emit = Context.MethodBuilder.Emitter;
			Label labelNull = emit.DefineLabel();
			Label labelEndIf = emit.DefineLabel();

			object[] attrs = pi.GetCustomAttributes(typeof(ParamNullValueAttribute), true);
			object nullValue = attrs.Length == 0 ? null : ((ParamNullValueAttribute)attrs[0]).Value;

			if (nullValue != null)
			{
				emit
					.ldarg_0
					.ldloc(_locManager)
					.ldloc(param)
					.callvirt(typeof(IDataParameter).GetProperty("Value").GetGetMethod())
					.ldloc(param)
					.callvirt(_baseType, "IsNull", _bindingFlags, typeof(DbManager), typeof(object), typeof(object))
					.brtrue(labelNull)
					;
			}

			if (type.IsEnum)
			{
				emit
					.ldloc(_locManager)
					.callvirt(typeof(DbManager).GetProperty("MappingSchema").GetGetMethod())
					.ldloc(param)
					.callvirt(typeof(IDataParameter).GetProperty("Value").GetGetMethod())
					.LoadType(type)
					.callvirt(typeof(MappingSchema), "MapValueToEnum", typeof(object), typeof(Type))
					.CastFromObject(type)
					;
			}
#if FW4
			else if (pi.IsRefCursor())
			{
				// Make sure the parameter is a List
				if (!type.GetInterfaces().Contains(typeof(IList)))
				{
					throw new Exception("The argument '" + pi.Name + "' must be of type 'IList'");
				}
				//Get the generic type of the list
				Type genericType = type.GetGenericArguments().First();


				// Get the data reader to the ref cursor
				var dataReader = emit.DeclareLocal(typeof(IDataReader));
				emit
					.ldloc(_locManager)
					.callvirt(typeof(DbManager).GetProperty("DataProvider").GetGetMethod())
					.ldloc(param)
					.callvirt(typeof(IDataParameter).GetProperty("Value").GetGetMethod())
					.callvirt(typeof(DataProviderBase), "GetRefCursorDataReader", typeof(object))
					.CastFromObject(typeof(IDataReader))
					.stloc(dataReader)
					;

				// Create the generic methos info to invoke
				var mapDataReaderToListMethodInfo = typeof (MappingSchema).GetMethod("MapDataReaderToList",
																						new[]
																							{
																								typeof (IDataReader),
																								typeof (object[])
																							})
					.MakeGenericMethod(genericType);

				// Run MapDataReaderToList
				emit
					.ldloc(_locManager)
					.callvirt(typeof(DbManager).GetProperty("MappingSchema").GetGetMethod())
					.ldloc(dataReader)
					.ldnull
					.callvirt(mapDataReaderToListMethodInfo)
					;
			}
#endif
			else
			{
				emit
					.ldarg_0
					.ldloc(_locManager)
					.ldloc(param)
					.callvirt(typeof(IDataParameter).GetProperty("Value").GetGetMethod())
					;

				MethodInfo converter = GetConverterMethod(type);

				if (converter == null)
				{
					emit
						.LoadType(type)
						.ldloc(param)
						.callvirt(_baseType, "ConvertChangeType", _bindingFlags, typeof(DbManager), typeof(object), typeof(Type), typeof(object))
						.unboxIfValueType(type)
						;
				}
				else
				{
					emit
						.ldloc(param)
						.callvirt(converter)
						;
				}
			}

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

				if (nullValue.GetType() != type || !emit.LoadWellKnownValue(nullValue))
				{
					string nullString = TypeDescriptor.GetConverter(type).ConvertToInvariantString(nullValue);
					FieldBuilder staticField = CreateNullValueField(type, nullString);

					emit
						.ldsfld(staticField)
						;
				}

				emit
					.MarkLabel(labelEndIf)
					;
			}

			emit.stind(type);
		}