public override System.Reflection.TypeInfo MapType(System.Reflection.TypeInfo type) { return(default(System.Reflection.TypeInfo)); }
private static object FlexibleChangeType(object objVal, System.Type targetType) { System.Reflection.TypeInfo ti = System.Reflection.IntrospectionExtensions.GetTypeInfo(targetType); bool typeIsNullable = (ti.IsGenericType && object.ReferenceEquals(targetType.GetGenericTypeDefinition(), typeof(System.Nullable <>))); bool typeCanBeAssignedNull = !ti.IsValueType || typeIsNullable; if (objVal == null || object.ReferenceEquals(objVal, System.DBNull.Value)) { if (typeCanBeAssignedNull) { return(null); } else { throw new System.ArgumentNullException("objVal ([DataSource] => SetProperty => FlexibleChangeType => you're trying to assign NULL to a type that NULL cannot be assigned to...)"); } } // End if (objVal == null || object.ReferenceEquals(objVal, System.DBNull.Value)) // Get base-type System.Type tThisType = objVal.GetType(); if (typeIsNullable) { targetType = System.Nullable.GetUnderlyingType(targetType); } // End if (typeIsNullable) if (object.ReferenceEquals(tThisType, targetType)) { return(objVal); } // Convert Guid => string if (object.ReferenceEquals(targetType, typeof(string)) && object.ReferenceEquals(tThisType, typeof(System.Guid))) { return(objVal.ToString()); } // End if (object.ReferenceEquals(targetType, typeof(string)) && object.ReferenceEquals(tThisType, typeof(System.Guid))) // Convert string => System.Net.IPAddress if (object.ReferenceEquals(targetType, typeof(System.Net.IPAddress)) && object.ReferenceEquals(tThisType, typeof(string))) { return(System.Net.IPAddress.Parse(objVal.ToString())); } // End if (object.ReferenceEquals(targetType, typeof(System.Net.IPAddress)) && object.ReferenceEquals(tThisType, typeof(string))) // Convert string => TimeSpan if (object.ReferenceEquals(targetType, typeof(System.TimeSpan)) && object.ReferenceEquals(tThisType, typeof(string))) { // https://stackoverflow.com/questions/11719055/why-does-timespan-parseexact-not-work // This is grotesque... ParseExact ignores the 12/24 hour convention... // return System.TimeSpan.ParseExact(objVal.ToString(), "HH':'mm':'ss", System.Globalization.CultureInfo.InvariantCulture); // Exception // return System.TimeSpan.ParseExact(objVal.ToString(), "hh\\:mm\\:ss", System.Globalization.CultureInfo.InvariantCulture); // This works, bc of lowercase ? // return System.TimeSpan.ParseExact(objVal.ToString(), "hh':'mm':'ss", System.Globalization.CultureInfo.InvariantCulture); // Yep, lowercase - no 24 notation... return(System.TimeSpan.Parse(objVal.ToString())); } // End if (object.ReferenceEquals(targetType, typeof(System.TimeSpan)) && object.ReferenceEquals(tThisType, typeof(string))) // Convert string => DateTime if (object.ReferenceEquals(targetType, typeof(System.DateTime)) && object.ReferenceEquals(tThisType, typeof(string))) { return(System.DateTime.Parse(objVal.ToString(), System.Globalization.CultureInfo.InvariantCulture)); } // End if (object.ReferenceEquals(targetType, typeof(System.DateTime)) && object.ReferenceEquals(tThisType, typeof(string))) // Convert string => Guid if (object.ReferenceEquals(targetType, typeof(System.Guid)) && object.ReferenceEquals(tThisType, typeof(string))) { return(new System.Guid(objVal.ToString())); } // End else if (object.ReferenceEquals(targetType, typeof(System.Guid)) && object.ReferenceEquals(tThisType, typeof(string))) return(System.Convert.ChangeType(objVal, targetType)); } // End Function FlexibleChangeType
public abstract System.Reflection.TypeInfo MapType(System.Reflection.TypeInfo type);
public virtual bool IsAssignableFrom(System.Reflection.TypeInfo typeInfo) { return(default(bool)); }
public override bool IsAssignableFrom(System.Reflection.TypeInfo typeInfo) { return(default(bool)); }
} // End Function IsSimpleType // [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] public T ConvertResult <T>(object objReturnValue, bool throwOnAssignNullToNonNullableType) { System.Type tReturnType = typeof(T); System.Reflection.TypeInfo ti = System.Reflection.IntrospectionExtensions.GetTypeInfo(tReturnType); bool typeIsNullable = (ti.IsGenericType && object.ReferenceEquals(tReturnType.GetGenericTypeDefinition(), typeof(System.Nullable <>))); bool typeCanBeAssignedNull = !ti.IsValueType || typeIsNullable; if (typeIsNullable) { tReturnType = System.Nullable.GetUnderlyingType(tReturnType); } // End if (typeIsNullable) // Catch & Normalize NULL if (objReturnValue == null || objReturnValue == System.DBNull.Value) { if (typeCanBeAssignedNull) { return((T)(object)null); } if (throwOnAssignNullToNonNullableType) { throw new System.IO.InvalidDataException("ConvertResult cannot assign NULL to non-nullable type."); } return(default(T)); } // End if (objReturnValue == null) System.Type tDataTypeOfScalarResult = objReturnValue.GetType(); if (object.ReferenceEquals(tReturnType, tDataTypeOfScalarResult)) { return((T)objReturnValue); } string strReturnValue = System.Convert.ToString(objReturnValue, System.Globalization.CultureInfo.InvariantCulture); try { if (object.ReferenceEquals(tReturnType, typeof(object))) { return((T)objReturnValue); } else if (object.ReferenceEquals(tReturnType, typeof(string))) { return((T)(object)strReturnValue); } // End if string else if (object.ReferenceEquals(tReturnType, typeof(bool))) { bool bReturnValue = false; if (bool.TryParse(strReturnValue, out bReturnValue)) { return((T)(object)bReturnValue); } if (strReturnValue == "0") { return((T)(object)false); } if (strReturnValue == "0.0") { return((T)(object)false); } return((T)(object)true); } // End if bool else if (object.ReferenceEquals(tReturnType, typeof(int))) { int iReturnValue; if (int.TryParse(strReturnValue, out iReturnValue)) { return((T)(object)iReturnValue); } throw new System.IO.InvalidDataException("ConvertResult: Returned data \"" + strReturnValue + "\" is not a valid integer."); } // End if int else if (object.ReferenceEquals(tReturnType, typeof(uint))) { uint uiReturnValue; if (uint.TryParse(strReturnValue, out uiReturnValue)) { return((T)(object)uiReturnValue); } throw new System.IO.InvalidDataException("ConvertResult: Returned data \"" + strReturnValue + "\" is not a valid unsigned integer."); } // End if uint else if (object.ReferenceEquals(tReturnType, typeof(long))) { long lngReturnValue; if (long.TryParse(strReturnValue, out lngReturnValue)) { return((T)(object)lngReturnValue); } throw new System.IO.InvalidDataException("ConvertResult: Returned data \"" + strReturnValue + "\" is not a valid unsigned integer."); } // End if long else if (object.ReferenceEquals(tReturnType, typeof(ulong))) { ulong ulngReturnValue; if (ulong.TryParse(strReturnValue, out ulngReturnValue)) { return((T)(object)ulngReturnValue); } throw new System.IO.InvalidDataException("ConvertResult: Returned data \"" + strReturnValue + "\" is not a valid unsigned long."); } // End if ulong else if (object.ReferenceEquals(tReturnType, typeof(float))) { float fltReturnValue; if (float.TryParse(strReturnValue, out fltReturnValue)) { return((T)(object)fltReturnValue); } throw new System.IO.InvalidDataException("ConvertResult: Returned data \"" + strReturnValue + "\" is not a valid float."); } else if (object.ReferenceEquals(tReturnType, typeof(double))) { double dblReturnValue; if (double.TryParse(strReturnValue, out dblReturnValue)) { return((T)(object)dblReturnValue); } throw new System.IO.InvalidDataException("ConvertResult: Returned data \"" + strReturnValue + "\" is not a valid double."); } else if (object.ReferenceEquals(tReturnType, typeof(System.Net.IPAddress))) { System.Net.IPAddress ipAddress = null; if (System.Net.IPAddress.TryParse(strReturnValue, out ipAddress)) { return((T)(object)ipAddress); } throw new System.IO.InvalidDataException("ConvertResult: Returned data \"" + strReturnValue + "\" is not a valid IP address."); } // End if IPAddress else if (object.ReferenceEquals(tReturnType, typeof(System.Guid))) { System.Guid retUID; try { retUID = new System.Guid(strReturnValue); return((T)(object)retUID); } catch (System.Exception ex) { } throw new System.IO.InvalidDataException("ConvertResult: Returned data \"" + strReturnValue + "\" is not a valid GUID."); } // End if System.Guid else // No datatype matches { throw new System.NotImplementedException("ConvertResult<T>: No implicit conversion operation defined for type \"" + tReturnType.FullName + "\"."); } // End else of if tReturnType = datatype } // End Try catch (System.Exception ex) { if (Log(ex)) { throw; } } // End Catch return(default(T)); }
protected virtual string GetResourcePrefix(System.Reflection.TypeInfo typeInfo, string?baseNamespace, string?resourcesRelativePath) { throw null; }
public static System.Reflection.InterfaceMapping GetRuntimeInterfaceMap(this System.Reflection.TypeInfo typeInfo, System.Type interfaceType) { return(default(System.Reflection.InterfaceMapping)); }
protected virtual bool IncludeType(System.Reflection.TypeInfo type) { throw null; }
protected virtual string GetResourcePrefix(System.Reflection.TypeInfo typeInfo) { throw null; }
public override bool IsAssignableFrom(System.Reflection.TypeInfo typeInfo) { throw null; }
public virtual bool IsAssignableFrom(System.Reflection.TypeInfo typeInfo) { throw null; }
protected virtual Microsoft.AspNetCore.Mvc.ApplicationModels.PageApplicationModel CreateModel(Microsoft.AspNetCore.Mvc.RazorPages.PageActionDescriptor actionDescriptor, System.Reflection.TypeInfo pageTypeInfo) { throw null; }
public override System.Reflection.TypeInfo MapType(System.Reflection.TypeInfo type) { throw null; }
public static Microsoft.Extensions.Internal.PropertyHelper[] GetVisibleProperties(System.Reflection.TypeInfo typeInfo) { throw null; }
internal static object Wrap(Type serviceInterfaceType, Func <string, System.Reflection.MethodInfo, object[], object> CallMethodAction, Func <string, System.Reflection.MethodInfo, object[], object> CallMethodAsyncAction) { //this method load GetCurrentMethod for xamarin linked assembly //System.Reflection.MethodBase fix = System.Reflection.MethodInfo.GetCurrentMethod(); System.Reflection.AssemblyName assemblyName = new System.Reflection.AssemblyName(string.Format("tmp_{0}", serviceInterfaceType.FullName)); string moduleName = string.Format("{0}.dll", assemblyName.Name); string ns = serviceInterfaceType.Namespace; if (!string.IsNullOrEmpty(ns)) { ns += "."; } ServiceContractAttribute attrib = serviceInterfaceType.GetCustomAttributes <ServiceContractAttribute>(true).Where(x => x.ServiceType == ServiceType.ServerService || x.ServiceType == ServiceType.ClientService || x.ServiceType == ServiceType.StreamService).FirstOrDefault(); #if (NET35) AssemblyBuilder assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); #elif (NET40) var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndCollect); #else AssemblyBuilder assembly = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndCollect); #endif #if (NETSTANDARD || NETCOREAPP || PORTABLE) ModuleBuilder module = assembly.DefineDynamicModule(moduleName); #else ModuleBuilder module = assembly.DefineDynamicModule(moduleName, false); #endif TypeBuilder type = module.DefineType(string.Format("{0}InterfaceWrapper_{1}", ns, serviceInterfaceType.Name), System.Reflection.TypeAttributes.Class | System.Reflection.TypeAttributes.AnsiClass | System.Reflection.TypeAttributes.Sealed | System.Reflection.TypeAttributes.NotPublic); type.AddInterfaceImplementation(serviceInterfaceType); // Define _Service0..N-1 private service fields FieldBuilder[] fields = new FieldBuilder[2]; #if (NETSTANDARD || NETCOREAPP) CustomAttributeBuilder cab = new CustomAttributeBuilder( System.Reflection.IntrospectionExtensions.GetTypeInfo(typeof(DebuggerBrowsableAttribute)).GetConstructor(new Type[] { typeof(DebuggerBrowsableState) }), new object[] { DebuggerBrowsableState.Never }); #else CustomAttributeBuilder cab = new CustomAttributeBuilder( typeof(DebuggerBrowsableAttribute).GetConstructor(new Type[] { typeof(DebuggerBrowsableState) }), new object[] { DebuggerBrowsableState.Never }); #endif fields[0] = type.DefineField(string.Format("_Service{0}", 0), serviceInterfaceType, System.Reflection.FieldAttributes.Public); fields[1] = type.DefineField(string.Format("_Service{0}", 1), serviceInterfaceType, System.Reflection.FieldAttributes.Public); // Ensure the field don't show up in the debugger tooltips fields[0].SetCustomAttribute(cab); fields[1].SetCustomAttribute(cab); // Define a simple constructor that takes all our services as arguments ConstructorBuilder ctor = type.DefineConstructor(System.Reflection.MethodAttributes.Public, System.Reflection.CallingConventions.HasThis, new Type[] { CallMethodAction.GetType(), CallMethodAsyncAction.GetType() }); ILGenerator generator = ctor.GetILGenerator(); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldarg_1); generator.Emit(OpCodes.Stfld, fields[0]); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldarg_2); generator.Emit(OpCodes.Stfld, fields[1]); generator.Emit(OpCodes.Ret); foreach (Type serviceType in GetFullTypes(serviceInterfaceType)) { // Implement all the methods of the interface foreach (System.Reflection.MethodInfo method in serviceType.GetListOfMethods()) { //generator.Emit(OpCodes.Pop); System.Reflection.ParameterInfo[] args = method.GetParameters(); MethodBuilder methodImpl = type.DefineMethod(method.Name, System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.Virtual, method.ReturnType, (from arg in args select arg.ParameterType).ToArray()); for (int i = 0; i < args.Length; i++) { ParameterBuilder parameterBuilder = methodImpl.DefineParameter(i + 1, System.Reflection.ParameterAttributes.None, args[i].Name); } // Generate code to simply call down into each service object // Any return values are discarded, except the last one, which is returned generator = methodImpl.GetILGenerator(); System.Reflection.MethodInfo invoke = null; if (method.ReturnType.GetBaseType() == typeof(Task)) { invoke = CallMethodAsyncAction.GetType().FindMethod("Invoke"); generator.Emit(OpCodes.Ldarg_0); //stack [this] generator.Emit(OpCodes.Ldfld, fields[1]); //stack } else { invoke = CallMethodAction.GetType().FindMethod("Invoke"); generator.Emit(OpCodes.Ldarg_0); //stack [this] generator.Emit(OpCodes.Ldfld, fields[0]); //stack } if (attrib == null) { throw new Exception("attrib not found"); } string serviceName = attrib.Name; if (attrib.ServiceType == ServiceType.ClientService) { serviceName = attrib.GetServiceName(false); } //add name of service generator.Emit(OpCodes.Ldstr, serviceName); System.Reflection.MethodInfo getCurgntMethod = typeof(System.Reflection.MethodBase).FindMethod("GetCurrentMethod"); if (getCurgntMethod == null) { throw new Exception("GetCurrentMethod not found"); } //add current method info generator.Emit(OpCodes.Call, getCurgntMethod); //add obj[] argumants if (args.Length > 0) { EmitInt32(generator, args.Length); generator.Emit(OpCodes.Newarr, typeof(object)); for (int index = 0; index < args.Length; index++) { generator.Emit(OpCodes.Dup); EmitInt32(generator, index); switch (index) { case 0: generator.Emit(OpCodes.Ldarg_1); break; case 1: generator.Emit(OpCodes.Ldarg_2); break; case 2: generator.Emit(OpCodes.Ldarg_3); break; default: generator.Emit(index < 255 ? OpCodes.Ldarg_S : OpCodes.Ldarg, index + 1); break; } //generator.Emit(OpCodes.Ldstr, args[index].Name); generator.Emit(OpCodes.Box, args[index].ParameterType); generator.Emit(OpCodes.Stelem_Ref); } } else { EmitInt32(generator, 0); generator.Emit(OpCodes.Newarr, typeof(object)); } if (invoke == null) { throw new Exception("invoke not found"); } generator.EmitCall(OpCodes.Call, invoke, null); if (method.ReturnType == typeof(void)) { generator.Emit(OpCodes.Pop); } else { generator.Emit(OpCodes.Castclass, method.ReturnType); generator.Emit(OpCodes.Unbox_Any, method.ReturnType); } //generator.Emit(OpCodes.Castclass, method.ReturnType); generator.Emit(OpCodes.Ret); } } #if (NETSTANDARD || NETCOREAPP || PORTABLE) System.Reflection.TypeInfo newType = type.CreateTypeInfo(); return(Activator.CreateInstance(newType.AsType(), CallMethodAction, CallMethodAsyncAction)); #else Type newType = type.CreateType(); return(Activator.CreateInstance(newType, CallMethodAction, CallMethodAsyncAction)); #endif }
public virtual T ExecuteScalar <T>(System.Data.IDbCommand cmd, bool throwOnAssignNullToNonNullableType) { object objReturnValue = null; string strReturnValue = null; System.Type tReturnType = typeof(T); System.Reflection.TypeInfo ti = System.Reflection.IntrospectionExtensions.GetTypeInfo(tReturnType); bool typeIsNullable = (ti.IsGenericType && object.ReferenceEquals(tReturnType.GetGenericTypeDefinition(), typeof(System.Nullable <>))); bool typeCanBeAssignedNull = !ti.IsValueType || typeIsNullable; if (typeIsNullable) { tReturnType = System.Nullable.GetUnderlyingType(tReturnType); } // End if (typeIsNullable) using (System.Data.IDbConnection idbc = this.Connection) { cmd.Connection = idbc; try { if (cmd.Connection.State != System.Data.ConnectionState.Open) { cmd.Connection.Open(); } objReturnValue = cmd.ExecuteScalar(); } // End Try catch (System.Data.Common.DbException ex) { if (Log("cDAL.ExecuteScalar<T>(string strSQL)", ex, cmd)) { throw; } } // End Catch finally { if (cmd.Connection.State != System.Data.ConnectionState.Closed) { cmd.Connection.Close(); } } // End Finally } // End using idbc // Catch & Normalize NULL if (objReturnValue == null || objReturnValue == System.DBNull.Value) { if (typeCanBeAssignedNull) { return((T)(object)null); } if (throwOnAssignNullToNonNullableType) { throw new System.IO.InvalidDataException("ExecuteScalar cannot assign NULL to non-nullable type."); } return(default(T)); } // End if (objReturnValue == null) System.Type tDataTypeOfScalarResult = objReturnValue.GetType(); if (object.ReferenceEquals(tReturnType, tDataTypeOfScalarResult)) { return((T)objReturnValue); } strReturnValue = System.Convert.ToString(objReturnValue, System.Globalization.CultureInfo.InvariantCulture); objReturnValue = null; try { if (object.ReferenceEquals(tReturnType, typeof(object))) { return((T)objReturnValue); } else if (object.ReferenceEquals(tReturnType, typeof(string))) { return((T)(object)strReturnValue); } // End if string else if (object.ReferenceEquals(tReturnType, typeof(bool))) { bool bReturnValue = false; if (bool.TryParse(strReturnValue, out bReturnValue)) { return((T)(object)bReturnValue); } if (strReturnValue == "0") { return((T)(object)false); } if (strReturnValue == "0.0") { return((T)(object)false); } return((T)(object)true); } // End if bool else if (object.ReferenceEquals(tReturnType, typeof(int))) { int iReturnValue; if (int.TryParse(strReturnValue, out iReturnValue)) { return((T)(object)iReturnValue); } throw new System.IO.InvalidDataException("ExecuteScalar: Returned data \"" + strReturnValue + "\" is not a valid integer."); } // End if int else if (object.ReferenceEquals(tReturnType, typeof(uint))) { uint uiReturnValue; if (uint.TryParse(strReturnValue, out uiReturnValue)) { return((T)(object)uiReturnValue); } throw new System.IO.InvalidDataException("ExecuteScalar: Returned data \"" + strReturnValue + "\" is not a valid unsigned integer."); } // End if uint else if (object.ReferenceEquals(tReturnType, typeof(long))) { long lngReturnValue; if (long.TryParse(strReturnValue, out lngReturnValue)) { return((T)(object)lngReturnValue); } throw new System.IO.InvalidDataException("ExecuteScalar: Returned data \"" + strReturnValue + "\" is not a valid unsigned integer."); } // End if long else if (object.ReferenceEquals(tReturnType, typeof(ulong))) { ulong ulngReturnValue; if (ulong.TryParse(strReturnValue, out ulngReturnValue)) { return((T)(object)ulngReturnValue); } throw new System.IO.InvalidDataException("ExecuteScalar: Returned data \"" + strReturnValue + "\" is not a valid unsigned long."); } // End if ulong else if (object.ReferenceEquals(tReturnType, typeof(float))) { float fltReturnValue; if (float.TryParse(strReturnValue, out fltReturnValue)) { return((T)(object)fltReturnValue); } throw new System.IO.InvalidDataException("ExecuteScalar: Returned data \"" + strReturnValue + "\" is not a valid float."); } else if (object.ReferenceEquals(tReturnType, typeof(double))) { double dblReturnValue; if (double.TryParse(strReturnValue, out dblReturnValue)) { return((T)(object)dblReturnValue); } throw new System.IO.InvalidDataException("ExecuteScalar: Returned data \"" + strReturnValue + "\" is not a valid double."); } else if (object.ReferenceEquals(tReturnType, typeof(System.Net.IPAddress))) { System.Net.IPAddress ipAddress = null; if (System.Net.IPAddress.TryParse(strReturnValue, out ipAddress)) { return((T)(object)ipAddress); } throw new System.IO.InvalidDataException("ExecuteScalar: Returned data \"" + strReturnValue + "\" is not a valid IP address."); } // End if IPAddress else if (object.ReferenceEquals(tReturnType, typeof(System.Guid))) { System.Guid retUID; if (System.Guid.TryParse(strReturnValue, out retUID)) { return((T)(object)retUID); } throw new System.IO.InvalidDataException("ExecuteScalar: Returned data \"" + strReturnValue + "\" is not a valid GUID."); } // End if System.Guid else // No datatype matches { throw new System.NotImplementedException("ExecuteScalar<T>: No implicit conversion operation defined for type \"" + tReturnType.FullName + "\"."); } // End else of if tReturnType = datatype } // End Try catch (System.Exception ex) { if (Log("cDAL.cs ==> ExecuteScalar<T>(string strSQL)", ex, cmd)) { throw; } } // End Catch return((T)(object)null); } // End Function ExecuteScalar(cmd)