Beispiel #1
0
        //public static void CallDynamic(ILGenerator il, MethodInfo method)
        //{
        //il.Emit(OpCodes.Callvirt, method);

        /*if (method.IsAbstract || method.IsVirtual)
         * {
         *  il.Emit(OpCodes.Callvirt, method);
         * }
         * else
         * {
         *  il.Emit(OpCodes.Call, method);
         * }*/
        /* Console.WriteLine(string.Format("{0} IsStatic:{1}", method.Name, method.IsStatic));
         * if (method.IsStatic)
         * {
         *   il.Emit(OpCodes.Call, method);
         * }
         * else
         * {
         *   il.Emit(OpCodes.Callvirt, method);
         * }
         * }*/
        /*
         * public static void CallDynamic(ILGenerator il, MethodInfo method,Type objType)
         * {
         *  if (objType == method.DeclaringType)
         *      il.Emit(OpCodes.Call, method);
         *  else
         *      il.Emit(OpCodes.Callvirt, method);
         * }*/

        public static bool EmitSymbolGet(ILGenerator il, SymbolInfo symbol)
        {
            if (symbol is SymbolVar)
            {
                var symbolVar = symbol as SymbolVar;
                EmitHelper.LoadVar(il, symbolVar.VarBuilder);
            }
            else if (symbol is SymbolArg)
            {
                SymbolArg argsymbol = (symbol as SymbolArg);
                il.Emit(OpCodes.Ldarg, argsymbol.ArgIndex);
            }
            else if (symbol is SymbolEnumItem)
            {
                SymbolEnumItem eisymbol = (symbol as SymbolEnumItem);
                EmitHelper.LoadInt(il, (int)eisymbol.EnumValue);
            }
            else if (symbol is SymbolPropertyDirect)
            {
                SymbolPropertyDirect prsymbol  = symbol as SymbolPropertyDirect;
                MethodInfo           getMethod = prsymbol.ExProperty.Property.GetGetMethod();
                EmitHelper.CallDynamic(il, getMethod, prsymbol.ExProperty.IsSelf);
            }
            else if (symbol is SymbolFieldDirect)
            {
                SymbolFieldDirect fymbol = symbol as SymbolFieldDirect;
                EmitHelper.LoadField(il, fymbol.ExField.Field);
            }
            else if (symbol is SymbolDefProperty)
            {
                SymbolDefProperty prsymbol = (symbol as SymbolDefProperty);
                if (!prsymbol.IsStatic)
                {
                    il.Emit(OpCodes.Ldarg_0);
                }
                MethodInfo getMethod = prsymbol.GetProperty().GetGetMethod();
                EmitHelper.CallDynamic(il, getMethod, true);
            }
            else if (symbol is SymbolDefField)
            {
                SymbolDefField prsymbol = (symbol as SymbolDefField);
                if (!prsymbol.IsStatic)
                {
                    il.Emit(OpCodes.Ldarg_0);
                }
                //il.Emit(OpCodes.Ldfld, prsymbol.Builder);
                EmitHelper.LoadField(il, prsymbol.GetField());
            }
            else if (symbol is SymbolDefClass)
            {
                return(true);
            }
            else
            {
                return(false);
            }
            return(true);
        }
Beispiel #2
0
 Exp analyAsDirectMember(AnalyExpContext context)
 {
     object[] objs = ClassContextHelper.SearchDirectIdent(classContext, IdentName);
     if (objs.Length == 0)
     {
         //error("属性或者规定的值'" + IdentName + "'不存在");
         return(null);
     }
     else if (objs.Length > 1)
     {
         error("属性或者规定的值'" + IdentName + "'不明确");
         return(null);
     }
     else
     {
         var        obj = objs[0];
         SymbolInfo directSymbol;
         if (obj is ExPropertyInfo)
         {
             var pvar = obj as ExPropertyInfo;
             directSymbol = new SymbolPropertyDirect(IdentName, pvar);
             RetType      = pvar.Property.PropertyType;
         }
         else if (obj is ExFieldInfo)
         {
             var pvar = obj as ExFieldInfo;
             directSymbol = new SymbolFieldDirect(IdentName, pvar);
             RetType      = pvar.Field.FieldType;
         }
         else
         {
             var enumSymbol = new SymbolEnumItem(IdentName, obj);
             directSymbol = enumSymbol;
             RetType      = enumSymbol.DimType;
         }
         if (directSymbol != null)
         {
             DirectMemberExp exp = new DirectMemberExp(this, IdentToken, directSymbol, this.IsAssignedBy, this.IsAssignedValue);
             return(exp);
         }
     }
     return(null);
 }
        public List <SymbolEnumItem> SearchEnumItem(string name)
        {
            List <SymbolEnumItem> list = new List <SymbolEnumItem>();

            foreach (var gcl in this.EnumTypes)
            {
                if (gcl is TktGcl)
                {
                    var fields = gcl.ForType.GetFields(BindingFlags.Static | BindingFlags.Public);
                    foreach (var fi in fields)
                    {
                        if (fi.Name == name)
                        {
                            object         value  = fi.GetValue(null);
                            SymbolEnumItem symbol = new SymbolEnumItem(fi.Name, value);
                            list.Add(symbol);
                        }
                    }
                }
                else
                {
                    var fields = gcl.ForType.GetFields(BindingFlags.Static | BindingFlags.Public);
                    foreach (var fi in fields)
                    {
                        object value      = fi.GetValue(null);
                        string rname      = fi.Name;
                        var    mapAttrObj = Attribute.GetCustomAttribute(fi, typeof(ZCodeAttribute));
                        if (mapAttrObj != null)
                        {
                            ZCodeAttribute mapAttr = mapAttrObj as ZCodeAttribute;
                            rname = mapAttr.Code;
                        }
                        if (rname == name)
                        {
                            SymbolEnumItem symbol = new SymbolEnumItem(rname, value);
                            list.Add(symbol);
                        }
                    }
                }
            }
            return(list);
        }