Esempio n. 1
0
        public static GenGetter CreateGetMethod(PropertyInfo pi)
        {
            var classType = typeof(T);
            var propType  = pi.PropertyType;
            var propName  = pi.Name;

            Dictionary <Type, Dictionary <string, GenGetter> > i1 = null;

            if (dGets.TryGetValue(classType, out i1))
            {
                Dictionary <string, GenGetter> i2 = null;
                if (i1.TryGetValue(propType, out i2))
                {
                    GenGetter i3 = null;
                    if (i2.TryGetValue(propName, out i3))
                    {
                        return(i3);
                    }
                }
            }

            //If there is no getter, return nothing
            var getMethod = pi.GetGetMethod();

            if (getMethod == null)
            {
                return(null);
            }

            //Create the dynamic method to wrap the internal get method
            var arguments = new Type[1] {
                classType
            };

            var getter    = new DynamicMethod(String.Concat("_Get", pi.Name, "_"), typeof(object), new Type[] { typeof(T) }, classType);
            var generator = getter.GetILGenerator();

            generator.DeclareLocal(propType);
            generator.Emit(OpCodes.Ldarg_0);
            generator.Emit(OpCodes.Castclass, classType);
            generator.EmitCall(OpCodes.Callvirt, getMethod, null);
            if (!propType.IsClass)
            {
                generator.Emit(OpCodes.Box, propType);
            }
            generator.Emit(OpCodes.Ret);

            //Create the delegate and return it
            GenGetter genGetter = (GenGetter)getter.CreateDelegate(typeof(GenGetter));

            Dictionary <Type, Dictionary <string, GenGetter> > tempDict = null;
            Dictionary <string, GenGetter> tempPropDict = null;

            if (!dGets.ContainsKey(classType))
            {
                tempPropDict = new Dictionary <string, GenGetter>();
                tempPropDict.Add(propName, genGetter);
                tempDict = new Dictionary <Type, Dictionary <string, GenGetter> >();
                tempDict.Add(propType, tempPropDict);
                dGets.Add(classType, tempDict);
            }
            else
            {
                if (!dGets[classType].ContainsKey(propType))
                {
                    tempPropDict = new Dictionary <string, GenGetter>();
                    tempPropDict.Add(propName, genGetter);
                    dGets[classType].Add(propType, tempPropDict);
                }
                else
                {
                    if (!dGets[classType][propType].ContainsKey(propName))
                    {
                        dGets[classType][propType].Add(propName, genGetter);
                    }
                }
            }
            return(genGetter);
        }
Esempio n. 2
0
        public static GenGetter CreateGetMethod(PropertyInfo pi)
        {
            //Create the locals needed.

            Type classType = typeof(T);

            Type returnType = typeof(K);

            string propertyName = pi.Name;



            //Let’s return the cached delegate if we have one.

            if (dGets.ContainsKey(classType))
            {
                if (dGets[classType].ContainsKey(returnType))
                {
                    if (dGets[classType][returnType].ContainsKey(propertyName))
                    {
                        return(dGets[classType][returnType][propertyName]);
                    }
                }
            }



            //If there is no getter, return nothing

            MethodInfo getMethod = pi.GetGetMethod();

            if (getMethod == null)
            {
                return(null);
            }



            //Create the dynamic method to wrap the internal get method

            Type[] arguments = new Type[1];

            arguments[0] = typeof(T);



            DynamicMethod getter = new DynamicMethod(

                String.Concat("Get", pi.Name, "_"),

                typeof(K),

                new Type[] { typeof(T) },

                pi.DeclaringType);

            ILGenerator gen = getter.GetILGenerator();

            gen.DeclareLocal(typeof(K));

            gen.Emit(OpCodes.Ldarg_0);

            gen.Emit(OpCodes.Castclass, pi.DeclaringType);

            gen.EmitCall(OpCodes.Callvirt, getMethod, null);

            gen.Emit(OpCodes.Ret);



            //Create the delegate and return it

            GenGetter genGetter = (GenGetter)getter.CreateDelegate(typeof(GenGetter));



            //Cache the delegate for future use.

            Dictionary <Type, Dictionary <string, GenGetter> > tempDict = null;

            Dictionary <string, GenGetter> tempPropDict = null;

            if (!dGets.ContainsKey(classType))
            {
                tempPropDict = new Dictionary <string, GenGetter>();

                tempPropDict.Add(propertyName, genGetter);

                tempDict = new Dictionary <Type, Dictionary <string, GenGetter> >();

                tempDict.Add(returnType, tempPropDict);

                dGets.Add(classType, tempDict);
            }

            else
            {
                if (!dGets[classType].ContainsKey(returnType))
                {
                    tempPropDict = new Dictionary <string, GenGetter>();

                    tempPropDict.Add(propertyName, genGetter);

                    dGets[classType].Add(returnType, tempPropDict);
                }

                else
                {
                    if (!dGets[classType][returnType].ContainsKey(propertyName))
                    {
                        dGets[classType][returnType].Add(propertyName, genGetter);
                    }
                }
            }

            //Return delegate to the caller.

            return(genGetter);
        }