private IEnumerable<Action<IComponentContext, object>> BuildLoggerInjectors(IReflect componentType) { //寻找类型为 "ILogger" 并且具有set方法的属性。 var loggerProperties = componentType .GetProperties(BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance) .Select(p => new { PropertyInfo = p, p.PropertyType, IndexParameters = p.GetIndexParameters(), Accessors = p.GetAccessors(false) }) .Where(x => x.PropertyType == typeof(ILogger)) //必须是一个日志记录器 .Where(x => !x.IndexParameters.Any()) //没有索引器 .Where(x => x.Accessors.Length != 1 || x.Accessors[0].ReturnType == typeof(void)); //必须具有set方法。 return loggerProperties.Select(entry => entry.PropertyInfo).Select(propertyInfo => (Action<IComponentContext, object>)((ctx, instance) => { var component = componentType.ToString(); var logger = _loggerCache.GetOrAdd(component, key => ctx.Resolve<ILogger>(new TypedParameter(typeof(Type), componentType))); propertyInfo.SetValue(instance, logger, null); })); }
private IEnumerable <Action <IComponentContext, object> > BuildLoggerInjectors(IReflect componentType) { //寻找类型为 "ILogger" 并且具有set方法的属性。 var loggerProperties = componentType .GetProperties(BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance) .Select(p => new { PropertyInfo = p, p.PropertyType, IndexParameters = p.GetIndexParameters(), Accessors = p.GetAccessors(false) }) .Where(x => x.PropertyType == typeof(ILogger)) //必须是一个日志记录器 .Where(x => !x.IndexParameters.Any()) //没有索引器 .Where(x => x.Accessors.Length != 1 || x.Accessors[0].ReturnType == typeof(void)); //必须具有set方法。 return(loggerProperties.Select(entry => entry.PropertyInfo).Select(propertyInfo => (Action <IComponentContext, object>)((ctx, instance) => { var component = componentType.ToString(); var logger = _loggerCache.GetOrAdd(component, key => ctx.Resolve <ILogger>(new TypedParameter(typeof(Type), componentType))); propertyInfo.SetValue(instance, logger, null); }))); }
internal static String ToTypeName(IReflect ir){ if (ir is ClassScope) return ((ClassScope)ir).GetName(); else if (ir is JSObject) return ((JSObject)ir).GetClassName(); else if (ir is GlobalScope) return "Global Object"; else{ Debug.Assert(ir is Type || ir is TypedArray); return ir.ToString(); } }
private ILogger GetCachedLogger(IReflect componentType, IComponentContext context) { return(_cache.GetOrAdd(componentType.ToString(), value => context.Resolve <ILogger>(new TypedParameter(typeof(Type), componentType)))); }
private static void ProcessMethods(IReflect type, IDictionary <string, Func <object, MoParams, IMoValue> > functions) { var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance); foreach (var method in methods) { var functionAttribute = method.GetCustomAttribute <MoFunctionAttribute>(); if (functionAttribute == null) { continue; } foreach (var name in functionAttribute.Name) { if (functions.ContainsKey(name)) { Debug.WriteLine($"Duplicate function \'{name}\' in {type.ToString()}"); continue; } IMoValue ExecuteMolangFunction(object instance, MoParams mo) { var methodParams = method.GetParameters(); IMoValue value = DoubleValue.Zero; object[] parameters = new object[methodParams.Length]; //List<object> parameters = new List<object>(); if (methodParams.Length == 1 && methodParams[0].ParameterType == typeof(MoParams)) { parameters[0] = mo; //parameters.Add(mo); } else { for (var index = 0; index < methodParams.Length; index++) { var parameter = methodParams[index]; if (!mo.Contains(index)) { if (!parameter.IsOptional) { throw new MissingMethodException($"Missing parameter: {parameter.Name}"); } break; } var t = parameter.ParameterType; if (t == typeof(MoParams)) { parameters[index] = mo; //.Add(mo); } else if (t == typeof(int)) { parameters[index] = mo.GetInt(index); } else if (t == typeof(double)) { parameters[index] = mo.GetDouble(index); } else if (t == typeof(float)) { parameters[index] = (float)mo.GetDouble(index); } else if (t == typeof(string)) { parameters[index] = mo.GetString(index); } else if (typeof(IMoStruct).IsAssignableFrom(t)) { parameters[index] = mo.GetStruct(index); } else if (typeof(MoLangEnvironment).IsAssignableFrom(t)) { parameters[index] = mo.GetEnv(index); } else { throw new Exception("Unknown parameter type."); } //TODO: Continue. } } var result = method.Invoke(instance, parameters); if (result != null) { if (result is IMoValue moValue) { return(moValue); } return(MoValue.FromObject(result)); } return(value); } functions.Add(name, ExecuteMolangFunction); } } }
internal static string ToTypeName(IReflect ir) { if (ir is ClassScope) { return ((ClassScope) ir).GetName(); } if (ir is JSObject) { return ((JSObject) ir).GetClassName(); } if (ir is GlobalScope) { return "Global Object"; } return ir.ToString(); }