/// <summary> /// Initializes a new instance of the <see cref="DbProvider"/> class. /// </summary> /// <param name="dbMetadata">The db metadata.</param> public DbProvider(IDbMetadata dbMetadata) { this.dbMetadata = dbMetadata; newCommand = DynamicConstructor.Create(dbMetadata.CommandType.GetConstructor(Type.EmptyTypes)); newConnection = DynamicConstructor.Create(dbMetadata.ConnectionType.GetConstructor(Type.EmptyTypes)); newCommandBuilder = DynamicConstructor.Create(dbMetadata.CommandBuilderType.GetConstructor(Type.EmptyTypes)); newDataAdapter = DynamicConstructor.Create(dbMetadata.DataAdapterType.GetConstructor(Type.EmptyTypes)); newParameter = DynamicConstructor.Create(dbMetadata.ParameterType.GetConstructor(Type.EmptyTypes)); }
/// <summary> /// Uses reflection to create an object using its /// default constructor. /// </summary> /// <param name="objectType">Type of object to create.</param> public static object CreateInstance(Type objectType) { DynamicConstructor cachedConstructor = MethodCaller.GetCachedConstructor(objectType); if (cachedConstructor == null) { throw new NotImplementedException("Default Constructor not implemented."); } return(cachedConstructor()); }
/// <summary> /// Initializes a new instance of the <see cref="DbProvider"/> class. /// </summary> /// <param name="dbMetadata">The db metadata.</param> public DbProvider(IDbMetadata dbMetadata) { this.dbMetadata = dbMetadata; newCommand = DynamicConstructor.Create(dbMetadata.CommandType.GetConstructor(Type.EmptyTypes)); // Oracle needs custom bind by name property set to true as it's false by default var bindByNameProperty = dbMetadata.CommandType.GetProperty("BindByName"); if (bindByNameProperty != null && bindByNameProperty.CanWrite) { commandBindByName = DynamicProperty.Create(bindByNameProperty); } newConnection = DynamicConstructor.Create(dbMetadata.ConnectionType.GetConstructor(Type.EmptyTypes)); newCommandBuilder = DynamicConstructor.Create(dbMetadata.CommandBuilderType.GetConstructor(Type.EmptyTypes)); newDataAdapter = DynamicConstructor.Create(dbMetadata.DataAdapterType.GetConstructor(Type.EmptyTypes)); newParameter = DynamicConstructor.Create(dbMetadata.ParameterType.GetConstructor(Type.EmptyTypes)); }
private static DynamicConstructor GetCachedConstructor(Type objectType) { DynamicConstructor dynamicConstructor = null; if (MethodCaller._ctorCache.TryGetValue(objectType, out dynamicConstructor)) { return(dynamicConstructor); } lock (MethodCaller._ctorCache) { if (!MethodCaller._ctorCache.TryGetValue(objectType, out dynamicConstructor)) { ConstructorInfo constructor = objectType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); dynamicConstructor = DynamicMethodHandlerFactory.CreateConstructor(constructor); MethodCaller._ctorCache.Add(objectType, dynamicConstructor); } } return(dynamicConstructor); }
private static DynamicConstructor GetCachedConstructor(Type objectType) { DynamicConstructor result = null; if (_ctorCache.TryGetValue(objectType, out result)) { return(result); } lock (_ctorCache) { if (!_ctorCache.TryGetValue(objectType, out result)) { ConstructorInfo info = objectType.GetConstructor(ctorFlags, null, Type.EmptyTypes, null); result = DynamicMethodHandlerFactory.CreateConstructor(info); _ctorCache.Add(objectType, result); } } return(result); }
public static void Main() { var handler = new DynamicConstructor(typeof(SampleHandler1)).New(); invokeIConsumeFor(handler, "Sample Message"); }