/// <summary> /// Extracts all needed data of any variable from the given StaticExtension by using reflection and appends it to the given container. /// </summary> /// <param name="container">the container where the data is appended to.</param> /// <param name="staticExtension">the StaticExtension that should be searched by using reflection.</param> private void ExtractVariableData(IStaticExtensionContainer container, IStaticExtension staticExtension) { Type staticExtensionType = staticExtension.GetType(); // get all relevant properties that are marked by a StaticVariableAttribute IEnumerable <PropertyInfo> listOfVariableProperties = staticExtensionType.GetProperties().Where(p => p.GetCustomAttribute <StaticVariableAttribute>() != null); foreach (var variableProperty in listOfVariableProperties) { if (variableProperty.MemberType != MemberTypes.Property) { throw new LibraryPluginManagerException(this, String.Format( "The member '{0}' in class '{1}' is not a property. Only properties are supported as static variables.", variableProperty.Name, staticExtensionType.Name)); } if (variableProperty.CanRead == false) { throw new LibraryPluginManagerException(this, String.Format( "The property '{0}' in class '{1}' doesn't have a getter. Only readable properties are supported as static variables.", variableProperty.Name, staticExtensionType.Name)); } if (SUPPORTED_TYPES.Contains(variableProperty.PropertyType) == false) { throw new LibraryPluginManagerException(this, String.Format( "The type '{2}' of property '{0}' in class '{1}' is not supported.", variableProperty.Name, staticExtensionType.Name, variableProperty.PropertyType)); } IStaticExtensionVariableData variableData = new StaticExtensionVariableData(); variableData.StaticExtension = staticExtension; variableData.PropertyName = variableProperty.Name; variableData.Type = variableProperty.PropertyType; StaticVariableAttribute attribute = variableProperty.GetCustomAttribute <StaticVariableAttribute>(); // resolve the identifier that is used in synery if (String.IsNullOrEmpty(attribute.AlternativeIdentifier)) { // there is no alternative identifier -> take the original name of the property as variable identifier variableData.SyneryIdentifier = variableProperty.Name; } else { // take the alternative name variableData.SyneryIdentifier = attribute.AlternativeIdentifier; } // check whether the property has a public setter if (variableProperty.SetMethod != null && variableProperty.SetMethod.IsPublic == true) { variableData.HasSetter = true; } else { variableData.HasSetter = false; } container.Variables.Add(variableData); } }
/// <summary> /// Extracts all needed data of any function from the given StaticExtension by using reflection and appends it to the given container. /// </summary> /// <param name="container">the container where the data is appended to.</param> /// <param name="staticExtension">the StaticExtension that should be searched by using reflection.</param> /// <returns></returns> private void ExtractFunctionData(IStaticExtensionContainer container, IStaticExtension staticExtension) { Type staticExtensionType = staticExtension.GetType(); // get all relevant methods that are marked by a StaticFunctionAttribute IEnumerable <MethodInfo> listOfFunctionMethods = staticExtensionType.GetMethods().Where(m => m.GetCustomAttribute <StaticFunctionAttribute>() != null); foreach (var functionMethod in listOfFunctionMethods) { StaticFunctionAttribute attribute = functionMethod.GetCustomAttribute <StaticFunctionAttribute>(); IStaticExtensionFunctionData functionData = new StaticExtensionFunctionData(); functionData.StaticExtension = staticExtension; functionData.MethodName = functionMethod.Name; // resolve the identifier of the function that is used in synery if (String.IsNullOrEmpty(attribute.AlternativeIdentifier)) { // there is no alternative identifier -> take the original name of the method as function identifier functionData.SyneryIdentifier = functionMethod.Name; } else { // take the alternative identifier functionData.SyneryIdentifier = attribute.AlternativeIdentifier; } // resolve the return type of the method if (functionMethod.ReturnType == typeof(void)) { // the function doesn't return anything functionData.ReturnType = null; } else if (SUPPORTED_TYPES.Contains(functionMethod.ReturnType) == true) { functionData.ReturnType = functionMethod.ReturnType; } else { throw new LibraryPluginManagerException(this, String.Format( "The return type '{0}' of method '{1}' in class '{2}' is not supported.", functionMethod.ReturnType.Name, functionMethod.Name, staticExtensionType.Name)); } // resolve the parameters foreach (var parameter in functionMethod.GetParameters()) { // validate the parameter if (parameter.IsOut) { throw new LibraryPluginManagerException(this, String.Format( "The parameter '{0}' of method '{1}' in class '{2}' is marked as out parameter. Out parameters are not supported.", parameter.Name, functionMethod.Name, staticExtensionType.Name)); } if (parameter.IsRetval) { throw new LibraryPluginManagerException(this, String.Format( "The parameter '{0}' of method '{1}' in class '{2}' is marked as Retval parameter. Retval parameters are not supported.", parameter.Name, functionMethod.Name, staticExtensionType.Name)); } if (SUPPORTED_TYPES.Contains(parameter.ParameterType) == false) { throw new LibraryPluginManagerException(this, String.Format( "The type '{3}' of parameter '{0}' of method '{1}' in class '{2}' is not supported.", parameter.Name, functionMethod.Name, staticExtensionType.Name, parameter.ParameterType.Name)); } // resolve neeeded parameter information IStaticExtensionFunctionParameterData parameterData = new StaticExtensionFunctionParameterData(); parameterData.Name = parameter.Name; parameterData.Type = parameter.ParameterType; parameterData.IsOptional = parameter.IsOptional; functionData.Parameters.Add(parameterData); } container.Functions.Add(functionData); } }