Пример #1
0
        private UserMethod DefineInterfaceMethod(UserType type, string name, string _return, string[] parTypes, string[] parNames)
        {
            MethodAttributes attr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Abstract | MethodAttributes.NewSlot | MethodAttributes.Virtual;

            IdEntry[] pars = new IdEntry[parTypes.Length];
            for (int i = 0; i < parTypes.Length; i++)
            {
                pars[i] = this.GetTypeIdEntry(parTypes[i]);
            }
            IdEntry ret = KeywordToType(_return) != null ? new IdEntry()
            {
                ReflectionObject = KeywordToType(_return)
            } : this.GetTypeIdEntry(_return);

            UserMethod metb = type.DefineMethod(name, attr, ret, pars, parNames);

            return(metb);
        }
Пример #2
0
        private UserMethod DefineMethod(UserType type, String name, List <string> modifiers, String _return, String[] parTypes, String[] parNames)
        {
            bool overrid = modifiers.Remove("override");

            if (overrid)
            {
                modifiers.Add("virtual");
            }
            if (!type.IsAbstract && modifiers.Contains("abstract"))
            {
                throw new CheckerException("Cannot declare abstract method " + name + " in a non-abstract class: " + type.Name);
            }
            if (modifiers.Contains("abstract"))
            {
                modifiers.Add("virtual");
            }
            MethodAttributes attr = MethodAttributes.HideBySig;

            foreach (String s in modifiers)
            {
                attr |= (MethodAttributes)Enum.Parse(typeof(MethodAttributes), s, true);
            }

            IdEntry[] pars = new IdEntry[parTypes.Length];
            for (int i = 0; i < parTypes.Length; i++)
            {
                pars[i] = this.GetTypeIdEntry(parTypes[i]);
            }
            IdEntry ret = KeywordToType(_return) != null ? new IdEntry()
            {
                ReflectionObject = KeywordToType(_return)
            } : this.GetTypeIdEntry(_return);

            if (overrid)
            {
                MethodInfo m = type.BaseType.GetMethod(name, BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, LibraryChecker.ToArray <Type>(pars), null);
                if (m == null || m.ReturnType != ret.ReflectionObject)
                {
                    throw new CheckerException("No suitable method found to override: " + name);
                }
                if (!m.IsVirtual)
                {
                    throw new CheckerException("Cannot override a non-virtual method: " + m.Name);
                }
            }
            else
            {
                MethodInfo m = type.GetMethod(name, BindingFlags.FlattenHierarchy | BindingFlags.ExactBinding | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, LibraryChecker.ToArray <Type>(pars), null);
                if (m != null)
                {
                    throw new CheckerException("Method already exists: " + m.Name);
                }
                if (type.GetInterfaceMethod(name, LibraryChecker.ToArray <Type>(pars)) != null)
                {
                    attr |= MethodAttributes.NewSlot | MethodAttributes.Virtual;
                }
            }
            UserMethod metb = type.DefineMethod(name, attr, ret, pars, parNames);

            return(metb);
        }