예제 #1
0
        public void Complex()
        {
            MethodParameter p    = MethodParameter.Create <int>();
            var             code = p.GetHashCode();

            MethodKey mk     = new MethodKey(typeof(AppContext), "test", null, null, false);
            var       mkCode = mk.GetHashCode();
        }
 public void RefResolveMethodOneParamter_Int()
 {
     for (int i = 0; i < 3; i++)
     {
         MethodInfo methodInfo = typeof(SampleCls).ResolveMethod("GetCount", new MethodParameter[] {
             MethodParameter.Create <int>(true)
         });
         Assert.IsNotNull(methodInfo);
         Assert.AreEqual("GetCount", methodInfo.Name);
         Assert.AreEqual(1, methodInfo.GetParameters().Length);
         Assert.AreEqual(typeof(int).MakeByRefType(), methodInfo.GetParameters()[0].ParameterType);
     }
 }
        public void RefResolveMethodOneParamter_string_Int()
        {
            MethodInfo methodInfo = typeof(SampleCls).ResolveMethod("GetCount", new Type[] { typeof(string) }, new MethodParameter[] {
                MethodParameter.CreateGeneric <string>(true),
                MethodParameter.Create <int>()
            });

            Assert.IsNotNull(methodInfo);
            Assert.AreEqual("GetCount", methodInfo.Name);
            Assert.AreEqual(2, methodInfo.GetParameters().Length);
            Assert.AreEqual(true, methodInfo.GetParameters()[0].ParameterType.IsByRef);
            Assert.AreEqual(typeof(string), methodInfo.GetParameters()[0].ParameterType.GetElementType());

            Assert.AreEqual(typeof(int), methodInfo.GetParameters()[1].ParameterType);
        }
        public static void WriteElement(MethodGenerator g, string dtoMemberName, Type elementType, Action loadValue)
        {
            var method = typeof(IDTOWriter).ResolveMethod("WriteElement",
                                                          new Type[] { elementType },
                                                          MethodParameter.Create <string>(),
                                                          MethodParameter.Create <bool>(),
                                                          MethodParameter.CreateGeneric(elementType));
            var prmIndex = SerializationArgs.WriterIndex;

            g.Call(method, () =>
            {
                g.LoadParameter(prmIndex);
                g.Load(dtoMemberName);
                g.Load(IsPrimitive(elementType));
                loadValue();
            });
        }
        private static MethodInfo GetMethodBySimple(Type type, SerializationMethodType methodType)
        {
            MethodInfo method = null;

            switch (methodType)
            {
            case SerializationMethodType.Serialize:
                if (type.Name.StartsWith("Nullable"))
                {
                    Type[] typeArgs = new Type[] { typeof(string), type };
                    method = typeof(IDTOWriter).ResolveMethod("Write", typeArgs);
                }
                else
                {
                    Type[] typeArgs = new Type[] { typeof(string), type };
                    method = typeof(IDTOWriter).ResolveMethod("Write", typeArgs);
                    if ((method == null) && (type.IsPrimitive == false))
                    {
                        //如果不是int、long等基础类型,而有可能是自定义类型,那么用以下代码得到方法
                        method = typeof(IDTOWriter).ResolveMethod("Write", _writeObjectArgs);
                    }
                }
                break;

            case SerializationMethodType.Deserialize:
                if (type.Name.StartsWith("Nullable"))
                {
                    Type[] targs      = type.GetGenericArguments();
                    string methodName = string.Format("ReadNullable{0}", targs[0].Name);
                    method = typeof(IDTOReader).ResolveMethod(methodName, _readArgs);
                }
                else
                {
                    Type[] targs      = type.GetGenericArguments();
                    string methodName = string.Format("Read{0}", type.Name);
                    method = typeof(IDTOReader).ResolveMethod(methodName, _readArgs);
                }
                if ((method == null) && (type.IsPrimitive == false))
                {
                    //如果不是int、long等基础类型,而有可能是自定义类型,那么用以下代码得到方法
                    //IDTOWriter.ReadObject<T>(string name);
                    method = typeof(IDTOReader).ResolveMethod("ReadObject", new Type[] { type }, MethodParameter.Create <string>());
                }
                break;
            }

            if (method == null)
            {
                throw new DTOException("没有找到" + type.ResolveName() + "的dto序列化方法");
            }

            return(method);
        }
        public static void ReadElement(MethodGenerator g, string dtoMemberName, Type elementType, IVariable index)
        {
            var method   = typeof(IDTOReader).ResolveMethod("ReadElement", new Type[] { elementType }, MethodParameter.Create <string>(), MethodParameter.Create <int>());
            var prmIndex = SerializationArgs.ReaderIndex;

            g.Call(method, () =>
            {
                g.LoadParameter(prmIndex);
                g.Load(dtoMemberName);
                g.LoadVariable(index, LoadOptions.Default);
            });
        }
        public void GenericResolveMethod_Generic()
        {
            MethodInfo methodInfo = typeof(SampleCls).ResolveMethod("GetCount", 1
                                                                    , new MethodParameter[] { MethodParameter.CreateGeneric(), MethodParameter.Create <string>() });

            Assert.IsNotNull(methodInfo);
            Assert.AreEqual("GetCount", methodInfo.Name);
            Assert.AreEqual(2, methodInfo.GetParameters().Length);
            Assert.AreEqual("T", methodInfo.GetParameters()[0].ParameterType.Name);
            Assert.AreEqual(typeof(string), methodInfo.GetParameters()[1].ParameterType);

            Assert.AreEqual(1, methodInfo.GetGenericArguments().Length);
        }
        public void GenericResolveMethodOneParamterArgumentInheritSame_One_Int_String()
        {
            MethodInfo methodInfo = typeof(SampleInheritCls).ResolveMethod("GetCount", new Type[] { typeof(int) }
                                                                           , new MethodParameter[] { MethodParameter.CreateGeneric <int>(), MethodParameter.Create <string>() });

            Assert.IsNotNull(methodInfo);
            Assert.AreEqual("GetCount", methodInfo.Name);
            Assert.AreEqual(2, methodInfo.GetParameters().Length);
            Assert.AreEqual(typeof(int), methodInfo.GetParameters()[0].ParameterType);
            Assert.AreEqual(typeof(string), methodInfo.GetParameters()[1].ParameterType);

            SampleInheritCls cls  = new SampleInheritCls();
            string           text = (string)methodInfo.Invoke(cls, new object[] { 1, string.Empty });

            Assert.AreEqual("SampleInheritCls", text);
        }
        public void GenericResolveMethodOneParamterArgument_One_Int_String()
        {
            MethodInfo methodInfo = typeof(SampleCls).ResolveMethod("GetCount", new Type[] { typeof(int) }
                                                                    , new MethodParameter[] { MethodParameter.CreateGeneric <int>(), MethodParameter.Create <string>() });

            Assert.IsNotNull(methodInfo);
            Assert.AreEqual("GetCount", methodInfo.Name);
            Assert.AreEqual(2, methodInfo.GetParameters().Length);
            Assert.AreEqual(typeof(int), methodInfo.GetParameters()[0].ParameterType);
            Assert.AreEqual(typeof(string), methodInfo.GetParameters()[1].ParameterType);

            Assert.AreEqual(1, methodInfo.GetGenericArguments().Length);
            Assert.AreEqual(typeof(int), methodInfo.GetGenericArguments()[0]);
        }