public void RegisterCommand() { Commands.Clear(); Dictionary <string, string> commandAliases = new Dictionary <string, string>(); List <Command> commands = ClassLoader.Load <Command>().ToList(); commands = commands.OrderBy(t => t.Name).ToList(); foreach (Command cl in commands) { if (!cl.CommandSystemID.Equals("") && !cl.CommandSystemID.Equals(CommandID)) { return; } string name = cl.Name; if (name == null) { Logger.WriteLine("Command " + cl.GetType().Name + " has no name!", LogLevel.WARNING); continue; } if (Commands.ContainsKey(name.ToLower()) && cl.GetType().GetCustomAttribute(typeof(OverrideCommandAttribute), false) == null) { Logger.WriteLine(name + " already exists, please remove or rename!", LogLevel.WARNING); continue; } if (cl.Aliases == null || cl.Aliases.Count == 0) { Logger.WriteLine("Command " + name + " has no aliases! Using " + name.ToLower() + " as Alias"); cl.Aliases.Add(name.ToLower()); } foreach (string alias in cl.Aliases.ToArray()) { if (commandAliases.ContainsKey(alias)) { if (name.Equals(commandAliases[alias])) { Logger.WriteLine("The Command " + name + " has duplicate alias entries.", LogLevel.WARNING); cl.Aliases.RemoveAll(t => t == alias); cl.Aliases.Add(alias); } else { Logger.WriteLine("The alias " + alias + " for Command " + name + " is already in use by Command " + commandAliases[alias] + ", removing!", LogLevel.WARNING); cl.Aliases.RemoveAll(t => t == alias); } continue; } commandAliases.Add(alias, name); } cl.SetParentSystem(this); Commands.Add(name.ToLower(), cl); } }
public Type Parse(string simplifyQualifiedName) { //var namespaceAliasName = simplifyQualifiedName.LeftOf("."); var array = simplifyQualifiedName.Split(','); var fullName = array[0].Split('.'); var qualifiedName = namespaceTable[fullName[0]] + "." + fullName[1] + "," + assemblyNameTable[array[1]]; return(ClassLoader.Load(qualifiedName)); }
/// <summary> /// 批量注册AppDomain下的程序集内符合特定条件的实体到数据表的映射关系 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="typeFilter"></param> /// <returns></returns> public DbConfiguration AddFromAppDomain(System.Func <Type, bool> typeFilter) { Assembly[] asms = null; if (NLiteEnvironment.IsWeb) { var buildManagerType = ClassLoader.Load("System.Web.Compilation.BuildManager,System.Web"); Guard.NotNull(buildManagerType, "buildManagerType"); asms = (buildManagerType .GetMethod("GetReferencedAssemblies", BindingFlags.Public | BindingFlags.Static) .Invoke(null, null) as IEnumerable) .Cast <Assembly>() .ToArray(); } else { asms = AppDomain.CurrentDomain.GetAssemblies(); } foreach (var asm in asms) { AddFromAssembly(asm, typeFilter); } return(this); }
protected override void Load() { Loader.ReadAllFiles(); Localisers = ClassLoader.Load <IBaseLocaliser>(); }
protected override Expression VisitMethodCall(MethodCallExpression m) { m = base.VisitMethodCall(m) as MethodCallExpression; var lambda = MethodMapping.ConvertMember(m.Method); if (lambda != null) { var ef = lambda.Body.Unwrap(); var parms = new Dictionary <string, ParameterMapping>(lambda.Parameters.Count); var pn = m.Method.IsStatic ? 0 : -1; foreach (var p in lambda.Parameters) { parms.Add(p.Name, new ParameterMapping(pn++, p.Type)); } var pie = ef.Convert(wpi => { if (wpi.NodeType == ExpressionType.Parameter) { ParameterMapping n; if (parms.TryGetValue(((ParameterExpression)wpi).Name, out n)) { var tmp = n.Index < 0 ? m.Object : m.Arguments[n.Index]; if (tmp.Type != n.Type) { return(Expression.Convert(tmp, n.Type)); } return(tmp); } } return(wpi); }); if (m.Method.ReturnType != pie.Type) { pie = Expression.Convert(pie, m.Method.ReturnType); } return(Visit(pie)); } var methodName = m.Method.Name; var declaringType = m.Method.DeclaringType; if (declaringType == typeof(System.Convert)) { Expression operand = null; Type toType = null; if (m.Method.Name.StartsWith("To")) { toType = ClassLoader.Load("System." + m.Method.Name.Replace("To", "")); operand = m.Arguments[0]; } if (operand != null && toType != null && toType != Types.Object) { return(Expression.Call(MethodRepository.GetConvertMethod(operand.Type, toType), operand)); } } if (typeof(TypeConverter).IsAssignableFrom(declaringType)) { Expression operand = null; Type toType = null; if (methodName.StartsWith("ConvertFrom")) { var c = m.Object as ConstantExpression; Type converterType = null; if (c != null) { converterType = (m.Object as ConstantExpression).Value.GetType(); } else { var ma = m.Object as MemberExpression; if (ma != null) { c = ma.Expression as ConstantExpression; if (c == null) { throw new NotSupportedException(string.Format("The method '{0}' is not supported", m.Method.Name)); } converterType = ma.Member.GetGetter()(c.Value).GetType(); } } toType = ClassLoader.Load("System." + converterType.Name.Replace("Converter", "")); if (toType == null) { throw new NotSupportedException(string.Format("The method '{0}' is not supported", m.Method.Name)); } if (methodName == "ConvertFrom" && m.Arguments.Count == 1) { operand = m.Arguments[0]; } else if (methodName == "ConvertFromString" && m.Arguments.Count == 1) { operand = m.Arguments[0]; } else if (methodName == "ConvertFromInvariantString" && m.Arguments.Count == 1) { operand = m.Arguments[0]; } } else if (methodName == "ConvertTo" && m.Arguments.Count == 2) { operand = m.Arguments[0]; toType = (m.Arguments[1] as ConstantExpression).Value as Type; } else if (methodName == "ConvertToInvariantString" && m.Arguments.Count == 1) { operand = m.Arguments[0]; toType = Types.String; } else if (methodName == "ConvertToString" && m.Arguments.Count == 1) { operand = m.Arguments[0]; toType = Types.String; } if (operand != null && toType != null && toType != Types.Object) { return(Expression.Call(MethodRepository.GetConvertMethod(operand.Type, toType), operand)); } throw new NotSupportedException(string.Format("The method '{0}' is not supported", methodName)); } if (methodName == "Parse" && m.Method.IsStatic && (declaringType.IsValueType || declaringType.IsNullable()) && m.Arguments.Count == 1 && m.Method.ReturnType == m.Type) { Expression operand = m.Arguments[0]; Type toType = declaringType.IsNullable() ? Nullable.GetUnderlyingType(declaringType) : declaringType; return(Expression.Call(MethodRepository.GetConvertMethod(operand.Type, toType), operand)); } if (declaringType == Types.String) { if (methodName == "Concat") { return(BindConcat(m.Arguments.ToArray())); } if (methodName == "Join") { return(BindJoin(m)); } } if (methodName == "ContainsValue" && IsSameOrParent(typeof(IDictionary <,>), declaringType)) { var args = GetGenericArguments(declaringType, typeof(IDictionary <,>)); var minf = EnumerableMethods .First(s => s.Name == "Contains" && s.GetParameters().Length == 2) .MakeGenericMethod(args[1]); return(Expression.Call( minf, Expression.PropertyOrField(m.Object, "Values"), m.Arguments[0])); } if (methodName == "ContainsKey" && IsSameOrParent(typeof(IDictionary <,>), declaringType)) { var args = GetGenericArguments(declaringType, typeof(IDictionary <,>)); var minf = EnumerableMethods .First(s => s.Name == "Contains" && s.GetParameters().Length == 2) .MakeGenericMethod(args[1]); return(Expression.Call( minf, Expression.PropertyOrField(m.Object, "Keys"), m.Arguments[0])); } if (declaringType.FullName == DLinq.StrSqlMethhodsType && declaringType.Assembly.GetName().Name == DLinq.StrAssemblyName) { DLinq.Init(declaringType.Assembly); return(Visit(m)); } //if (methodName == "Like" && declaringType == typeof(SqlFunctions) && m.Arguments.Count == 5) //{ // return BindLike( // m.Arguments[0] // , m.Arguments[1] // , (bool)(m.Arguments[2] as ConstantExpression).Value // , (bool)(m.Arguments[3] as ConstantExpression).Value // , (bool)(m.Arguments[4] as ConstantExpression).Value); //} if (typeof(Queryable).IsAssignableFrom(declaringType) || typeof(Enumerable).IsAssignableFrom(declaringType)) { var elementType = NLite.Data.Linq.Internal.ReflectionHelper.GetElementType(m.Arguments[0].Type); switch (methodName) { case "Contains": EntityMapping mapping; if (DbContext.dbConfiguration.mappings.TryGetValue(elementType.TypeHandle.Value, out mapping)) { // Expression left = } break; //case "Aggregate": // { // var type = NLite.Reflection.TypeHelper.GetElementType(m.Arguments[0].Type); // if (type.IsNullable()) // type = Nullable.GetUnderlyingType(type); // if (type.IsClass && type != Types.String) // throw new NotSupportedException("Not support 'Aggregate' function for complex type."); // break; // } //case "ElementAt": // var index = m.Arguments[1]; // var elementType = NLite.Reflection.TypeHelper.GetElementType(m.Arguments[0].Type); // var c = index as ConstantExpression; // if (c != null) // { // if((int)c.Value == 0) // return Expression.Call(typeof(Enumerable), "Take", new Type[] { elementType }, m.Arguments[0], Expression.Constant(1, Types.Int32)); // index = Expression.Constant((int)c.Value + 1, Types.Int32); // } // else // index = Expression.Add(index, Expression.Constant(1, Types.Int32)); // var s = Expression.Call(typeof(Enumerable), "Skip", new Type[] { elementType },m.Arguments[0], index); // return Expression.Call(typeof(Enumerable), "Take", new Type[] { elementType },s, Expression.Constant(1,Types.Int32)); } } if (IsSameOrParent(typeof(IEnumerable <>), declaringType) && !declaringType.IsArray && m.Object != null && m.Object.NodeType == ExpressionType.Constant && !typeof(IQueryable).IsAssignableFrom(declaringType)) { switch (methodName) { case "Contains": var elementType = NLite.Data.Linq.Internal.ReflectionHelper.GetElementType(declaringType); if (!DbContext.dbConfiguration.HasClass(elementType) && elementType != Types.Char) { var lst = (m.Object as ConstantExpression).Value as IEnumerable; if (lst != null) { var items = lst.Cast <object>().ToArray(); var arry = Array.CreateInstance(elementType, items.Length); for (int i = 0; i < items.Length; i++) { arry.SetValue(items[i], i); } Expression array = Expression.Constant(arry); var containsMethod = EnumerableMethods .First(s => s.Name == "Contains" && s.GetParameters().Length == 2) .MakeGenericMethod(elementType); m = Expression.Call(containsMethod, array, m.Arguments[0]); } } break; } } return(m); }