public static void TestPseudoCustomAttributes() { MethodInfo m = typeof(ParametersWithPseudoCustomtAttributes).Project().GetTypeInfo().GetDeclaredMethod("Foo"); ParameterInfo[] pis = m.GetParameters(); { ParameterInfo p = pis[0]; CustomAttributeData cad = p.CustomAttributes.Single(c => c.AttributeType == typeof(InAttribute).Project()); InAttribute i = cad.UnprojectAndInstantiate <InAttribute>(); } { ParameterInfo p = pis[1]; CustomAttributeData cad = p.CustomAttributes.Single(c => c.AttributeType == typeof(OutAttribute).Project()); OutAttribute o = cad.UnprojectAndInstantiate <OutAttribute>(); } { ParameterInfo p = pis[2]; CustomAttributeData cad = p.CustomAttributes.Single(c => c.AttributeType == typeof(OptionalAttribute).Project()); OptionalAttribute o = cad.UnprojectAndInstantiate <OptionalAttribute>(); } { ParameterInfo p = pis[3]; CustomAttributeData cad = p.CustomAttributes.Single(c => c.AttributeType == typeof(MarshalAsAttribute).Project()); MarshalAsAttribute ma = cad.UnprojectAndInstantiate <MarshalAsAttribute>(); Assert.Equal(UnmanagedType.I4, ma.Value); } }
internal static string ExportReward(Reward reward, string prefix, int rewardIndex, bool skipLocalization = true) { StringBuilder result = new StringBuilder(); result.AppendLine($"{prefix}Reward_{rewardIndex}_Type {reward.Type}"); foreach (PropertyInfo prop in reward.GetType().GetProperties()) { if (!prop.CanRead || !prop.CanWrite) { continue; } string propName = prop.Name; SkipFieldAttribute skipPropA = prop.GetCustomAttribute <SkipFieldAttribute>(); if ((skipLocalization && propName == "Localization") || skipPropA != null) { continue; } object propValue = prop.GetValue(reward); NoValueAttribute noValueA = prop.GetCustomAttribute <NoValueAttribute>(); OptionalAttribute optionalA = prop.GetCustomAttribute <OptionalAttribute>(); if (skipPropA != null) { continue; } if (noValueA != null) { if (propValue.Equals(true)) { propValue = ""; } else { continue; } } if (prop.PropertyType.IsNullable()) { if (propValue == null) { if (optionalA != null) { if (optionalA.DefaultValue == null) { continue; } propValue = optionalA.DefaultValue; } } } result.AppendLine($"{prefix}Reward_{rewardIndex}_{propName} {propValue}"); } return(result.ToString()); }
internal static Attribute[] GetCustomAttributes(RuntimeParameterInfo parameter, RuntimeType caType, out int count) { count = 0; bool flag = caType == (RuntimeType)typeof(object) || caType == (RuntimeType)typeof(Attribute); if (!flag && PseudoCustomAttribute.s_pca.GetValueOrDefault(caType) == null) { return(null); } Attribute[] array = new Attribute[PseudoCustomAttribute.s_pcasCount]; if (flag || caType == (RuntimeType)typeof(InAttribute)) { Attribute customAttribute = InAttribute.GetCustomAttribute(parameter); if (customAttribute != null) { Attribute[] array2 = array; int num = count; count = num + 1; array2[num] = customAttribute; } } if (flag || caType == (RuntimeType)typeof(OutAttribute)) { Attribute customAttribute = OutAttribute.GetCustomAttribute(parameter); if (customAttribute != null) { Attribute[] array3 = array; int num = count; count = num + 1; array3[num] = customAttribute; } } if (flag || caType == (RuntimeType)typeof(OptionalAttribute)) { Attribute customAttribute = OptionalAttribute.GetCustomAttribute(parameter); if (customAttribute != null) { Attribute[] array4 = array; int num = count; count = num + 1; array4[num] = customAttribute; } } if (flag || caType == (RuntimeType)typeof(MarshalAsAttribute)) { Attribute customAttribute = MarshalAsAttribute.GetCustomAttribute(parameter); if (customAttribute != null) { Attribute[] array5 = array; int num = count; count = num + 1; array5[num] = customAttribute; } } return(array); }
internal object[] GetPseudoCustomAttributes() { int count = 0; if (IsIn) { count++; } if (IsOut) { count++; } if (IsOptional) { count++; } if (marshalAs != null) { count++; } if (count == 0) { return(null); } object[] attrs = new object [count]; count = 0; if (IsIn) { attrs [count++] = new InAttribute(); } if (IsOut) { attrs [count++] = new OutAttribute(); } if (IsOptional) { attrs [count++] = new OptionalAttribute(); } if (marshalAs != null) { #if NETCORE throw new NotImplementedException(); #else attrs [count++] = marshalAs.Copy(); #endif } return(attrs); }
internal object[] GetPseudoCustomAttributes() { int count = 0; if (IsIn) { count++; } if (IsOut) { count++; } if (IsOptional) { count++; } if (marshalAs != null) { count++; } if (count == 0) { return(null); } object[] attrs = new object [count]; count = 0; if (IsIn) { attrs [count++] = new InAttribute(); } if (IsOut) { attrs [count++] = new OutAttribute(); } if (IsOptional) { attrs [count++] = new OptionalAttribute(); } if (marshalAs != null) { #if NETCORE attrs [count++] = (MarshalAsAttribute)marshalAs.CloneInternal(); #else attrs [count++] = marshalAs.Copy(); #endif } return(attrs); }
internal object[] GetPseudoCustomAttributes() { int count = 0; if (IsIn) { count++; } if (IsOut) { count++; } if (IsOptional) { count++; } #if !MICRO_LIB if (marshalAs != null) { count++; } #endif if (count == 0) { return(null); } object[] attrs = new object [count]; count = 0; if (IsIn) { attrs [count++] = new InAttribute(); } if (IsOptional) { attrs [count++] = new OptionalAttribute(); } if (IsOut) { attrs [count++] = new OutAttribute(); } #if !MICRO_LIB if (marshalAs != null) { attrs [count++] = marshalAs.ToMarshalAsAttribute(); } #endif return(attrs); }
internal static Attribute[] GetCustomAttributes(ParameterInfo parameter, Type caType, out int count) { count = 0; bool flag = (caType == typeof(object)) || (caType == typeof(Attribute)); if (!flag && (s_pca[caType] == null)) { return(null); } Attribute[] attributeArray = new Attribute[s_pcasCount]; Attribute customAttribute = null; if (flag || (caType == typeof(InAttribute))) { customAttribute = InAttribute.GetCustomAttribute(parameter); if (customAttribute != null) { attributeArray[count++] = customAttribute; } } if (flag || (caType == typeof(OutAttribute))) { customAttribute = OutAttribute.GetCustomAttribute(parameter); if (customAttribute != null) { attributeArray[count++] = customAttribute; } } if (flag || (caType == typeof(OptionalAttribute))) { customAttribute = OptionalAttribute.GetCustomAttribute(parameter); if (customAttribute != null) { attributeArray[count++] = customAttribute; } } if (flag || (caType == typeof(MarshalAsAttribute))) { customAttribute = MarshalAsAttribute.GetCustomAttribute(parameter); if (customAttribute != null) { attributeArray[count++] = customAttribute; } } return(attributeArray); }
internal object[] GetPseudoCustomAttributes() { int num = 0; if (this.IsIn) { num++; } if (this.IsOut) { num++; } if (this.IsOptional) { num++; } if (this.marshalAs != null) { num++; } if (num == 0) { return(null); } object[] array = new object[num]; num = 0; if (this.IsIn) { array[num++] = new InAttribute(); } if (this.IsOptional) { array[num++] = new OptionalAttribute(); } if (this.IsOut) { array[num++] = new OutAttribute(); } if (this.marshalAs != null) { array[num++] = this.marshalAs.ToMarshalAsAttribute(); } return(array); }
private IThomasElement Instantiate(Type elementType) { // Ist bereits eine Instanz vorhanden? if (_drivers.ContainsInstance(elementType)) { return(_drivers.GetInstance(elementType)); } if (_sensors.ContainsInstance(elementType)) { return(_sensors.GetInstance(elementType)); } if (_actors.ContainsInstance(elementType)) { return(_actors.GetInstance(elementType)); } if (_controllers.ContainsInstance(elementType)) { return(_controllers.GetInstance(elementType)); } // Parameter, die der neuen Instanz übergeben werden List <object> instanceParameters = new List <object>(); // Erforderliche Abhängigkeiten. Sollte eine nicht verfügbar sein, ist dieses Element auch nicht verfügbar. RequirementAttribute requirementAttribute = (RequirementAttribute)elementType.GetCustomAttributes(typeof(RequirementAttribute), true).FirstOrDefault(); if (requirementAttribute != null) { foreach (Type subElementType in requirementAttribute.RequiredElements) { IThomasElement requiredInstance = Instantiate(subElementType); if (requiredInstance == null) { Logger.Warning($"Abhängigkeit {subElementType.Name} von {elementType.Name} konnte nicht erfüllt werden."); return(null); } instanceParameters.Add(requiredInstance); } } // Optionale Abhängigkeiten. Falls verfügbar, werden diese Komponenten dem aktuellen Element mit übergeben. OptionalAttribute optionalAttribute = (OptionalAttribute)elementType.GetCustomAttributes(typeof(OptionalAttribute), true).FirstOrDefault(); if (optionalAttribute != null) { foreach (Type subElementType in optionalAttribute.OptionalElements) { IThomasElement requiredInstance = Instantiate(subElementType); if (requiredInstance == null) { Logger.Warning($"Optionale Abhängigkeit {subElementType.Name} von {elementType.Name} konnte nicht erfüllt werden."); } instanceParameters.Add(requiredInstance); } } // Falls ein Konfigurationselement angefordert wird, gib dieses, falls vorhanden zurück. Sollte es angefordert werden, aber nicht vorhanden sein, gib Null zurück. ConfigAttribute configAttribute = (ConfigAttribute)elementType.GetCustomAttributes(typeof(ConfigAttribute), true).FirstOrDefault(); if (configAttribute != null) { instanceParameters.Add(_applicationConfig.ModuleConfigs.ContainsKey(configAttribute.Id) ? _applicationConfig.ModuleConfigs[configAttribute.Id] : null); } // Instanz erstellen IThomasElement instance = (IThomasElement)(instanceParameters.Count > 0 ? Activator.CreateInstance(elementType, instanceParameters.ToArray()) : Activator.CreateInstance(elementType)); // Sonderbehandlung von Treibern, da diese aufgrund von fehlender Hardware nicht initialisiert werden könnten. if (typeof(IDriver).IsAssignableFrom(elementType)) { if (_failedDrivers.Contains(elementType)) { return(null); } if (!((IDriver)instance).Initialize()) { Logger.Warning($"Treiber {((IDriver)instance).Name} konnte nicht initalisiert werden."); _failedDrivers.Add(elementType); return(null); } } // Instanz zu einem der Pools hinzufügen if (!_drivers.AddIfMatches(instance) && !_sensors.AddIfMatches(instance) && !_actors.AddIfMatches(instance) && !_controllers.AddIfMatches(instance)) { throw new ArgumentException("Ungültiger Element-Typ", nameof(elementType)); } return(instance); }
internal static bool IsDefined(RuntimeParameterInfo parameter, RuntimeType caType) { bool flag = caType == (RuntimeType)typeof(object) || caType == (RuntimeType)typeof(Attribute); return((flag || !(PseudoCustomAttribute.s_pca.GetValueOrDefault(caType) == null)) && (((flag || caType == (RuntimeType)typeof(InAttribute)) && InAttribute.IsDefined(parameter)) || ((flag || caType == (RuntimeType)typeof(OutAttribute)) && OutAttribute.IsDefined(parameter)) || ((flag || caType == (RuntimeType)typeof(OptionalAttribute)) && OptionalAttribute.IsDefined(parameter)) || ((flag || caType == (RuntimeType)typeof(MarshalAsAttribute)) && MarshalAsAttribute.IsDefined(parameter)))); }
internal static bool IsDefined(ParameterInfo parameter, Type caType) { bool flag = (caType == typeof(object)) || (caType == typeof(Attribute)); if (!flag && (s_pca[caType] == null)) { return(false); } return(((flag || (caType == typeof(InAttribute))) && InAttribute.IsDefined(parameter)) || (((flag || (caType == typeof(OutAttribute))) && OutAttribute.IsDefined(parameter)) || (((flag || (caType == typeof(OptionalAttribute))) && OptionalAttribute.IsDefined(parameter)) || ((flag || (caType == typeof(MarshalAsAttribute))) && MarshalAsAttribute.IsDefined(parameter))))); }
private static ExcelFunctionRegistration ToExcelFunctionRegistration(IScript script, string FunctionName) { var paramExprs = script .Parameters .Select(x => Expression.Parameter(GetExcelRegistrationTypeFor(x), x.Name)) .ToList(); var methodInfo = typeof(ExcelScriptAddin).GetMethod(nameof(InternalRun), BindingFlags.Static | BindingFlags.NonPublic); var paramsToObj = paramExprs.Select(x => Expression.Convert(x, typeof(object))); // cast parameter to object, otherwise won't match function signature of ExcelScriptAddin.Run var paramsArray = Expression.NewArrayInit(typeof(object), paramsToObj.ToArray()); var methodArgs = new Expression[] { Expression.Constant(script, typeof(IScript)), paramsArray }; LambdaExpression lambdaExpression = Expression.Lambda(Expression.Call(methodInfo, methodArgs), FunctionName, paramExprs); ExcelFunctionAttribute excelFunctionAttribute = new ExcelFunctionAttribute() { Name = FunctionName }; if (!String.IsNullOrEmpty(script.Description)) { excelFunctionAttribute.Description = script.Description; } IEnumerable <ExcelParameterRegistration> paramRegistrations = script .Parameters .Select((IParameter x) => { var argumentAttribute = new ExcelArgumentAttribute() { Name = x.Name }; var parameterRegistration = new ExcelParameterRegistration(argumentAttribute); if (x.Type == typeof(Excel.Range) || x.Type == typeof(ExcelReference)) { argumentAttribute.AllowReference = true; } if (x.IsOptional) { var optionalAttribute = new OptionalAttribute(); parameterRegistration.CustomAttributes.Add(optionalAttribute); var defaultValueAttribute = new DefaultParameterValueAttribute(x.DefaultValue); parameterRegistration.CustomAttributes.Add(defaultValueAttribute); } if (!String.IsNullOrEmpty(x.Description)) { argumentAttribute.Description = x.Description; } return(parameterRegistration); }); var reg = new ExcelFunctionRegistration(lambdaExpression, excelFunctionAttribute, paramRegistrations); return(reg); }
internal static bool IsDefined(RuntimeParameterInfo parameter, RuntimeType caType) { bool flag = (caType == ((RuntimeType)typeof(object))) || (caType == ((RuntimeType)typeof(Attribute))); if (!flag && (s_pca.GetValueOrDefault(caType) == null)) { return(false); } return(((flag || (caType == ((RuntimeType)typeof(InAttribute)))) && InAttribute.IsDefined(parameter)) || (((flag || (caType == ((RuntimeType)typeof(OutAttribute)))) && OutAttribute.IsDefined(parameter)) || (((flag || (caType == ((RuntimeType)typeof(OptionalAttribute)))) && OptionalAttribute.IsDefined(parameter)) || ((flag || (caType == ((RuntimeType)typeof(MarshalAsAttribute)))) && MarshalAsAttribute.IsDefined(parameter))))); }