public static IDataAccessor CreateInstance(string bindingMethod, DataAccessorInstanceType dataAccessorInstanceType, string instanceName) { try { if (dataAccessorInstanceType == DataAccessorInstanceType.Singleton && string.IsNullOrEmpty(instanceName)) { throw new EtkException("For 'Singleton', 'InstanceName' is mandatory"); } MethodInfo methodInfo = null; object callingInstance = null; switch (dataAccessorInstanceType) { case DataAccessorInstanceType.Singleton: case DataAccessorInstanceType.Static: methodInfo = TypeHelpers.GetMethod(null, bindingMethod); if (dataAccessorInstanceType == DataAccessorInstanceType.Singleton) { PropertyInfo pi = methodInfo.DeclaringType.GetProperties(BindingFlags.Static).FirstOrDefault(p => p.Name.Equals(instanceName)); if (pi != null) { callingInstance = pi.GetGetMethod().Invoke(null, null); } else { MethodInfo mi = methodInfo.DeclaringType.GetMethods(BindingFlags.Static).FirstOrDefault(m => m.Name.Equals(instanceName)); if (mi != null) { callingInstance = mi.Invoke(null, null); } else { throw new Exception("'InstanceName' not found"); } } } break; } DataAccessor dataAccessor = new DataAccessor(); dataAccessor.callingInstance = callingInstance; dataAccessor.InstanceType = dataAccessorInstanceType; dataAccessor.MethodInfo = methodInfo; ParameterInfo[] parameters = methodInfo.GetParameters(); dataAccessor.ParametersInfo = new List <ParameterInfo>(parameters == null ? new List <ParameterInfo>() : new List <ParameterInfo>(parameters)); dataAccessor.ReturnType = methodInfo.ReturnType; return(dataAccessor); } catch (Exception ex) { throw new EtkException($"Connat create Data Accessor for '{bindingMethod}':{ex.Message}"); } }
public static IModelAccessor CreateInstance(IModelAccessorGroup parent, XmlModelAccessor definition) { if (definition == null) { return(null); } ModelAccessor accessor = new ModelAccessor(); try { accessor.Parent = parent; accessor.Name = definition.Name.EmptyIfNull().Trim(); accessor.Ident = definition.Ident.EmptyIfNull().Trim(); if (string.IsNullOrEmpty(accessor.Ident)) { accessor.Ident = accessor.Name; } accessor.Description = definition.Description.EmptyIfNull().Trim(); accessor.modelType = definition.ReturnModelType.EmptyIfNull().Trim(); definition.InstanceName = definition.InstanceName.EmptyIfNull().Trim(); definition.InstanceType = definition.InstanceType.EmptyIfNull().Trim(); definition.Method = definition.Method.EmptyIfNull().Trim(); definition.Method = definition.Method.EmptyIfNull().Trim(); if (string.IsNullOrEmpty(accessor.Name)) { throw new EtkException("'Name' is mandatory"); } if (string.IsNullOrEmpty(definition.Method)) { throw new EtkException("'Method' is mandatory"); } DataAccessorInstanceType dataAccessorInstanceType = ModelManagement.DataAccessors.DataAccessor.AccessorInstanceTypeFrom(definition.InstanceType); accessor.DataAccessor = ModelManagement.DataAccessors.DataAccessor.CreateInstance(definition.Method, dataAccessorInstanceType, definition.InstanceName); } catch (Exception ex) { throw new EtkException($"Cannot create 'ModelAccessor' '{definition.Name.EmptyIfNull()} {definition.Method.EmptyIfNull()}': {ex.Message}"); } return(accessor); }