private void Test(Type type, string name, Type[] args, Type ethalonType)
 {
     var res = new ExtensionMethodResolver(new Dictionary<string, bool> {{"System", true}, {"System.Linq", true}});
     var found = res.FindExtensionMethod(type, name, args);
     var bucket = ethalonType.GetMethods().Where(m => m.Name == name).ToArray();
     Assert.Contains(found, bucket);
 }
Esempio n. 2
0
        public Context(LensCompilerOptions options = null)
        {
            Options = options ?? new LensCompilerOptions();

            _DefinedTypes = new Dictionary<string, TypeEntity>();
            _DefinedProperties = new Dictionary<string, GlobalPropertyInfo>();

            Namespaces = new Dictionary<string, bool>();
            if (Options.UseDefaultNamespaces)
            {
                Namespaces.Add("System", true);
                Namespaces.Add("System.Linq", true);
                Namespaces.Add("System.Text.RegularExpressions", true);
            }

            _TypeResolver = new TypeResolver(Namespaces);
            _TypeResolver.ExternalLookup = name =>
            {
                TypeEntity ent;
                _DefinedTypes.TryGetValue(name, out ent);
                return ent == null ? null : ent.TypeBuilder;
            };

            _ExtensionResolver = new ExtensionMethodResolver(Namespaces);

            var an = new AssemblyName(getAssemblyName());
            if (Options.AllowSave)
            {
                if(string.IsNullOrEmpty(Options.FileName))
                    Options.FileName = an.Name + (Options.SaveAsExe ? ".exe" : ".dll");

                MainAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.RunAndSave);
                MainModule = MainAssembly.DefineDynamicModule(an.Name, Options.FileName);
            }
            else
            {
                MainAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
                MainModule = MainAssembly.DefineDynamicModule(an.Name);
            }

            ContextId = GlobalPropertyHelper.RegisterContext();

            MainType = CreateType(EntityNames.MainTypeName);
            MainType.Kind = TypeEntityKind.Main;
            MainType.Interfaces = new[] {typeof (IScript)};
            MainMethod = MainType.CreateMethod(EntityNames.RunMethodName, typeof(object), Type.EmptyTypes, false, true);
            MainMethod.ReturnType = typeof (object);

            if(Options.LoadStandardLibrary)
                InitStdlib();

            InitSafeMode();
        }