示例#1
0
        private static MapperCommand GetCommand(MapperProvider provider, CommandItem item, params object[] source)
        {
            if (provider == null)
            {
                provider = MapperProvider.DefaultProvider;
            }
            List <ParameterSource> parameterSources = MapperCommand.ConvertParameterSource(provider, source);
            MapperCommand          mapperCommand    = new MapperCommand(provider)
            {
                commandId       = item.Id,
                parameterSource = parameterSources,
                Type            = item.Type,
                Text            = item.GetParameterMatchText(parameterSources),
                Result          = item.ResultType,
                Timeout         = item.Timeout
            };

            mapperCommand.BuildCommand();
            return(mapperCommand);
        }
示例#2
0
        public void ApplyParameter()
        {
            CommandItem commandItem = new CommandItem()
            {
                Text = this.Text
            };

            commandItem.Parse();
            if (this.parameterSource == null)
            {
                this.parameterSource = new List <ParameterSource>();
                this.parameterSource.AddRange(this.provider.Parameter);
                foreach (MapperParameterItem value in this.Parameter.Values)
                {
                    this.parameterSource.Add(new ParameterSource()
                    {
                        Name  = value.ParameterName,
                        Value = value.Value
                    });
                }
            }
            this.Text = commandItem.GetParameterMatchText(this.parameterSource);
        }
示例#3
0
        internal static Type BuildMapperAssembly(Type mapperInterfaceType)
        {
            object name;
            object obj;

            BaseMapper.logger.Debug(string.Concat("BuildMapperAssembly ", mapperInterfaceType.FullName), (LogInfo)null, null);
            string          str             = string.Concat(mapperInterfaceType.Namespace, ".Concrete");
            AppDomain       currentDomain   = AppDomain.CurrentDomain;
            AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(str), AssemblyBuilderAccess.Run);
            string          str1            = string.Concat(str, ".", mapperInterfaceType.Name.Substring(1));
            ModuleBuilder   moduleBuilder   = assemblyBuilder.DefineDynamicModule(str1);
            TypeBuilder     typeBuilder     = moduleBuilder.DefineType(str1, TypeAttributes.Public, BaseMapper.typeBaseMapper);
            NameAttribute   customAttribute = mapperInterfaceType.GetTypeInfo().GetCustomAttribute <NameAttribute>();

            if (customAttribute != null)
            {
                name = customAttribute.Name;
            }
            else
            {
                name = null;
            }
            if (name == null)
            {
                name = mapperInterfaceType.Name.Substring(1, mapperInterfaceType.Name.Length - "IMapper".Length);
            }
            string str2 = (string)name;

            typeBuilder.AddInterfaceImplementation(mapperInterfaceType);
            AnonymousTypeBuilder anonymousTypeBuilder = new AnonymousTypeBuilder(assemblyBuilder, moduleBuilder);

            MethodInfo[] methods = mapperInterfaceType.GetMethods();
            for (int i = 0; i < (int)methods.Length; i++)
            {
                MethodInfo      methodInfo = methods[i];
                ParameterInfo[] parameters = methodInfo.GetParameters();
                Type[]          array      = (
                    from p in (IEnumerable <ParameterInfo>)methodInfo.GetParameters()
                    select p.ParameterType).ToArray <Type>();
                List <ParameterAttribute> list          = methodInfo.GetCustomAttributes <ParameterAttribute>().ToList <ParameterAttribute>();
                MethodBuilder             methodBuilder = typeBuilder.DefineMethod(methodInfo.Name, MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual | MethodAttributes.HideBySig | MethodAttributes.VtableLayoutMask | MethodAttributes.NewSlot, CallingConventions.Standard, methodInfo.ReturnType, array);
                for (byte j = 0; j < (int)array.Length; j = (byte)(j + 1))
                {
                    methodBuilder.DefineParameter(j + 1, ParameterAttributes.None, parameters[j].Name);
                }
                ILGenerator lGenerator = methodBuilder.GetILGenerator();
                if (methodInfo.ReturnType != typeof(void))
                {
                    lGenerator.DeclareLocal(methodInfo.ReturnType);
                }
                lGenerator.Emit(OpCodes.Nop);
                lGenerator.Emit(OpCodes.Ldarg_0);
                lGenerator.Emit(OpCodes.Call, BaseMapper.typeBaseMapper.GetMethod("get_Provider", BindingFlags.Instance | BindingFlags.Public));
                NameAttribute nameAttribute = methodInfo.GetCustomAttribute <NameAttribute>();
                if (nameAttribute != null)
                {
                    obj = nameAttribute.Name;
                }
                else
                {
                    obj = null;
                }
                if (obj == null)
                {
                    obj = string.Concat(str2, ".", methodInfo.Name);
                }
                string str3 = (string)obj;
                lGenerator.Emit(OpCodes.Ldstr, str3);
                if (array.Length != 0 || list.Count != 0)
                {
                    sbyte  num       = 0;
                    bool[] flagArray = new bool[(int)array.Length + list.Count];
                    for (byte k = 0; k < (int)array.Length; k = (byte)(k + 1))
                    {
                        flagArray[k] = array[k].GetTypeInfo().IsSubclassOf(typeof(ValueObject));
                        if (flagArray[k])
                        {
                            num = (sbyte)(num + 1);
                        }
                    }
                    for (byte l = 0; l < list.Count; l = (byte)(l + 1))
                    {
                        flagArray[(int)array.Length + l] = list[l].Parameter.Value.GetType().GetTypeInfo().IsSubclassOf(typeof(ValueObject));
                        if (flagArray[(int)array.Length + l])
                        {
                            num = (sbyte)(num + 1);
                        }
                    }
                    Type[]   type          = new Type[(int)array.Length + list.Count - num];
                    string[] parameterName = new string[(int)array.Length + list.Count - num];
                    BaseMapper.EmitHepler(lGenerator, OpCodes.Ldc_I4_S, num + (type.Length == 0 ? 0 : 1));
                    lGenerator.Emit(OpCodes.Newarr, typeof(object));
                    lGenerator.Emit(OpCodes.Dup);
                    sbyte num1 = 0;
                    for (byte m = 0; m < (int)array.Length; m = (byte)(m + 1))
                    {
                        if (!flagArray[m])
                        {
                            type[m - num1]          = array[m];
                            parameterName[m - num1] = parameters[m].Name;
                        }
                        else
                        {
                            OpCode ldcI4S = OpCodes.Ldc_I4_S;
                            sbyte  num2   = num1;
                            num1 = (sbyte)(num2 + 1);
                            BaseMapper.EmitHepler(lGenerator, ldcI4S, num2);
                            BaseMapper.EmitHepler(lGenerator, OpCodes.Ldarg_S, m + 1);
                            lGenerator.Emit(OpCodes.Stelem_Ref);
                            if (m == (int)array.Length - (type.Length == 0 ? 2 : 1))
                            {
                                lGenerator.Emit(OpCodes.Dup);
                            }
                        }
                    }
                    for (byte n = 0; n < list.Count; n = (byte)(n + 1))
                    {
                        if (!flagArray[(int)array.Length + n])
                        {
                            BaseMapper.directParameterValue.Add(string.Concat(str1, ".", methodInfo.Name), list[n].Parameter.Value);
                            type[(int)array.Length + n - num1]          = list[n].Parameter.Value.GetType();
                            parameterName[(int)array.Length + n - num1] = list[n].Parameter.ParameterName;
                        }
                    }
                    if (type.Length != 0)
                    {
                        BaseMapper.EmitHepler(lGenerator, OpCodes.Ldc_I4_S, num);
                        for (byte o = 0; o < (int)array.Length; o = (byte)(o + 1))
                        {
                            if (!flagArray[o])
                            {
                                BaseMapper.EmitHepler(lGenerator, OpCodes.Ldarg_S, o + 1);
                            }
                        }
                        for (byte p1 = 0; p1 < list.Count; p1 = (byte)(p1 + 1))
                        {
                            if (!flagArray[(int)array.Length + p1])
                            {
                                lGenerator.Emit(OpCodes.Ldsfld, BaseMapper.fieldDirectParameterValue);
                                lGenerator.Emit(OpCodes.Ldstr, string.Concat(str1, ".", methodInfo.Name));
                                lGenerator.Emit(OpCodes.Callvirt, BaseMapper.methodDirectParameterValue);
                            }
                        }
                        Type type1 = anonymousTypeBuilder.CreateType(type, parameterName);
                        lGenerator.Emit(OpCodes.Newobj, type1.GetConstructors()[0]);
                        lGenerator.Emit(OpCodes.Stelem_Ref);
                    }
                }
                else
                {
                    MethodInfo method = typeof(Array).GetMethod("Empty");
                    method = method.MakeGenericMethod(new Type[] { typeof(object) });
                    lGenerator.Emit(OpCodes.Call, method);
                }
                CommandItem item = CommandRepository.Instance[str3] ?? new CommandItem()
                {
                    Verb = CommandVerb.Get
                };
                MethodInfo methodInfo1 = (methodInfo.ReturnType == typeof(void) || item.Verb != CommandVerb.Get ? BaseMapper.methodQuery : BaseMapper.methodScalar);
                TypeInfo   typeInfo    = methodInfo.ReturnType.GetTypeInfo();
                bool       flag        = typeInfo.ImplementedInterfaces.Contains <Type>(typeof(IList));
                bool       flag1       = typeInfo.ImplementedInterfaces.Contains <Type>(typeof(IDictionary));
                bool       flag2       = typeInfo.IsSubclassOf(typeof(ValueObject));
                bool       flag3       = false;
                if (methodInfo.ReturnType == typeof(ValueSet))
                {
                    methodInfo1 = BaseMapper.methodDataSet;
                }
                else if (methodInfo.ReturnType == typeof(ValueTable))
                {
                    methodInfo1 = BaseMapper.methodDataTable;
                }
                else if (methodInfo.ReturnType == typeof(ValueRow))
                {
                    methodInfo1 = BaseMapper.methodDataRow;
                }
                else if (item.Verb == CommandVerb.Get && flag | flag1 | flag2)
                {
                    methodInfo1 = BaseMapper.methodDataTable;
                    flag3       = true;
                }
                if (methodInfo1 == BaseMapper.methodScalar)
                {
                    methodInfo1 = methodInfo1.MakeGenericMethod(new Type[] { methodInfo.ReturnType });
                }
                lGenerator.Emit(OpCodes.Call, methodInfo1);
                if (flag3)
                {
                    MethodInfo methodInfo2 = null;
                    if (!flag)
                    {
                        methodInfo2 = (!flag1 ? BaseMapper.methodToFirstVo.MakeGenericMethod(new Type[] { methodInfo.ReturnType }) : BaseMapper.methodToDictionary.MakeGenericMethod(new Type[] { methodInfo.ReturnType.GenericTypeArguments[0], methodInfo.ReturnType.GenericTypeArguments[1] }));
                    }
                    else
                    {
                        methodInfo2 = BaseMapper.methodToList.MakeGenericMethod(new Type[] { methodInfo.ReturnType.GenericTypeArguments[0] });
                    }
                    lGenerator.Emit(OpCodes.Call, methodInfo2);
                }
                if (methodInfo.ReturnType != typeof(void))
                {
                    lGenerator.Emit(OpCodes.Stloc_0);
                    lGenerator.Emit(OpCodes.Br_S, (sbyte)0);
                    lGenerator.Emit(OpCodes.Ldloc_0);
                }
                else
                {
                    lGenerator.Emit(OpCodes.Pop);
                }
                lGenerator.Emit(OpCodes.Ret);
            }
            return(typeBuilder.CreateTypeInfo().AsType());
        }
示例#4
0
        internal static void AddCommand(string commandsXml)
        {
            object      value;
            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.LoadXml(commandsXml);
            XmlNode xmlNodes = xmlDocument.SelectSingleNode("/commands");

            if (xmlNodes == null)
            {
                return;
            }
            Dictionary <string, MatchItem> strs = new Dictionary <string, MatchItem>();

            foreach (XmlElement xmlElement in xmlNodes.SelectNodes("commands/*"))
            {
                CommandVerb name = (CommandVerb)((Code <CommandVerb>)xmlElement.Name);
                if (name == null)
                {
                    throw new Exception(string.Concat(xmlElement.Name, " wrong CommandVerb"));
                }
                if (name == null)
                {
                    continue;
                }
                XmlAttribute itemOf = xmlElement.Attributes["type"];
                if (itemOf != null)
                {
                    value = itemOf.Value;
                }
                else
                {
                    value = null;
                }
                if (value == null)
                {
                    value = "Text";
                }
                string      str         = (string)value;
                CommandItem commandItem = new CommandItem()
                {
                    Id   = xmlElement.Attributes["id"].Value,
                    Type = EnumExtension.Parse <Test.Core.CommandType>(str),
                    Verb = name,
                    Text = xmlElement.ChildNodes[0].InnerText.Trim()
                };
                commandItem.Parse();
                foreach (XmlElement xmlElement1 in xmlElement.SelectNodes("*"))
                {
                    MatchItem matchItem = ((MatchType)((Code <MatchType>)xmlElement1.Name)).CreateMatchItem();
                    if (matchItem == null)
                    {
                        throw new Exception(string.Concat(commandItem.Id, ".", xmlElement1.Name, " wrong MatchType"));
                    }
                    xmlElement1.ToVo <MatchItem>(matchItem);
                    if (matchItem.Type == null)
                    {
                        continue;
                    }
                    commandItem.Matches.Add(matchItem);
                }
                foreach (MatchItem value1 in strs.Values)
                {
                    commandItem.Matches.Add(value1);
                }
                if (!CommandRepository.Instance.commands.ContainsKey(commandItem.Id))
                {
                    CommandRepository.Instance.commands.Add(commandItem.Id, commandItem);
                }
                else
                {
                    CommandRepository.Instance.commands[commandItem.Id] = commandItem;
                }
            }
        }
示例#5
0
        internal static void CreateRepository(string path)
        {
            string value;
            object obj;
            object value1;
            object obj1;
            string str;

            try
            {
                try
                {
                    string[]          files             = Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories);
                    CommandRepository commandRepository = new CommandRepository();
                    string[]          strArrays         = files;
                    for (int i = 0; i < (int)strArrays.Length; i++)
                    {
                        string      str1        = strArrays[i];
                        XmlDocument xmlDocument = new XmlDocument();
                        FileStream  fileStream  = File.OpenRead(str1);
                        xmlDocument.Load(fileStream);
                        fileStream.Dispose();
                        XmlNode xmlNodes = xmlDocument.SelectSingleNode("/mapper");
                        if (xmlNodes != null)
                        {
                            XmlAttribute itemOf = xmlNodes.Attributes["provider"];
                            if (itemOf != null)
                            {
                                value = itemOf.Value;
                            }
                            else
                            {
                                value = null;
                            }
                            string           str2       = value;
                            List <MatchItem> matchItems = new List <MatchItem>();
                            foreach (XmlElement xmlElement in xmlNodes.SelectNodes("matches/*"))
                            {
                                MatchItem matchItem = ((MatchType)((Code <MatchType>)xmlElement.Name)).CreateMatchItem();
                                if (matchItem == null)
                                {
                                    throw new Exception(string.Concat(xmlElement.Name, " wrong MatchType"));
                                }
                                xmlElement.ToVo <MatchItem>(matchItem);
                                matchItems.Add(matchItem);
                            }
                            foreach (XmlElement xmlElement1 in xmlNodes.SelectNodes("commands/*"))
                            {
                                CommandVerb name = (CommandVerb)((Code <CommandVerb>)xmlElement1.Name);
                                if (name == null)
                                {
                                    throw new Exception(string.Concat(xmlElement1.Name, " wrong CommandVerb"));
                                }
                                if (name == null)
                                {
                                    continue;
                                }
                                XmlAttribute xmlAttribute = xmlElement1.Attributes["type"];
                                if (xmlAttribute != null)
                                {
                                    obj = xmlAttribute.Value;
                                }
                                else
                                {
                                    obj = null;
                                }
                                if (obj == null)
                                {
                                    obj = "Text";
                                }
                                string      str3        = (string)obj;
                                CommandItem commandItem = new CommandItem()
                                {
                                    Id   = xmlElement1.Attributes["id"].Value.Trim(),
                                    Type = EnumExtension.Parse <Test.Core.CommandType>(str3),
                                    Verb = name,
                                    Text = xmlElement1.ChildNodes[0].InnerText
                                };
                                XmlAttribute itemOf1 = xmlElement1.Attributes["provider"];
                                if (itemOf1 != null)
                                {
                                    value1 = itemOf1.Value;
                                }
                                else
                                {
                                    value1 = null;
                                }
                                if (value1 == null)
                                {
                                    value1 = str2;
                                }
                                commandItem.Provider = (string)value1;
                                XmlAttribute xmlAttribute1 = xmlElement1.Attributes["timeout"];
                                if (xmlAttribute1 != null)
                                {
                                    obj1 = xmlAttribute1.Value;
                                }
                                else
                                {
                                    obj1 = null;
                                }
                                if (obj1 == null)
                                {
                                    obj1 = "0";
                                }
                                commandItem.Timeout = short.Parse((string)obj1);
                                XmlAttribute itemOf2 = xmlElement1.Attributes["result"];
                                if (itemOf2 != null)
                                {
                                    str = itemOf2.Value;
                                }
                                else
                                {
                                    str = null;
                                }
                                object nothing = (CommandResult)((Code <CommandResult>)str);
                                if (nothing == null)
                                {
                                    nothing = BaseCode <CommandResult> .Nothing;
                                }
                                commandItem.ResultType = (CommandResult)nothing;
                                CommandItem commandItem1 = commandItem;
                                commandItem1.Parse();
                                foreach (XmlElement xmlElement2 in xmlElement1.SelectNodes("*"))
                                {
                                    MatchItem matchItem1 = ((MatchType)((Code <MatchType>)xmlElement2.Name)).CreateMatchItem();
                                    if (matchItem1 == null)
                                    {
                                        throw new Exception(string.Concat(commandItem1.Id, ".", xmlElement2.Name, " wrong MatchType"));
                                    }
                                    xmlElement2.ToVo <MatchItem>(matchItem1);
                                    if (matchItem1.Type == null)
                                    {
                                        continue;
                                    }
                                    commandItem1.Matches.Add(matchItem1);
                                }
                                foreach (MatchItem matchItem2 in matchItems)
                                {
                                    if (commandItem1.Matches.Find((MatchItem p) => p.Name == matchItem2.Name) != null)
                                    {
                                        continue;
                                    }
                                    commandItem1.Matches.Add(matchItem2);
                                }
                                if (!commandRepository.commands.ContainsKey(commandItem1.Id))
                                {
                                    commandRepository.commands.Add(commandItem1.Id, commandItem1);
                                }
                                else
                                {
                                    commandRepository.commands[commandItem1.Id] = commandItem1;
                                }
                            }
                        }
                    }
                    CommandRepository.Instance = commandRepository;
                    CommandRepository.logger.Info(string.Concat("resource loaded", Environment.NewLine, path), (LogInfo)null, null);
                }
                catch (Exception exception1)
                {
                    Exception exception = exception1;
                    if (CommandRepository.Instance != null)
                    {
                        CommandRepository.logger.Error("reloading fail, retry", exception);
                        CommandRepository.CreateRepository(path);
                    }
                    else
                    {
                        CommandRepository.logger.Fatal("loading fail", exception);
                        Thread.Sleep(1000);
                    }
                    throw exception;
                }
            }
            finally
            {
                (new Thread(() => {
                    (new FileSystemWatcher()
                    {
                        Path = path,
                        IncludeSubdirectories = true,
                        EnableRaisingEvents = true
                    }).WaitForChanged(WatcherChangeTypes.Changed);
                    Thread.Sleep(3000);
                    CommandRepository.CreateRepository(path);
                })).Start();
            }
        }