예제 #1
0
        /// <summary>
        /// Initialize <see cref="TypeServices" /> object that provides services from TypeSystem to given owner.
        /// </summary>
        /// <param name="owner">Owner which uses current <see cref="TypeServices" />.</param>
        /// <param name="manager">Representation of AppDomain where assemblies are stored.</param>
        internal TypeServices(AssemblyProvider owner, AssembliesManager manager)
        {
            _owner   = owner;
            _manager = manager;

            //every assembly has to have runtime as prioritized reference
            //note that adding Runtime reference to itself is not a problem
            References.Add(Settings.Runtime);
            //assembly used for resolving is also owner itself
            References.Add(owner.Key);
        }
        /// <summary>
        ///     Initializes a new instance of the <see cref="CSharpScriptFile" /> class.
        /// </summary>
        public CSharpScriptFile()
        {
            ReferencedAssemblies.Add("Microsoft.CSharp.dll");
            ReferencedAssemblies.Add("System.dll");
            ReferencedAssemblies.Add("System.Core.dll");
            ReferencedAssemblies.Add("System.Data.dll");
            ReferencedAssemblies.Add("System.Linq.dll");
            ReferencedAssemblies.Add("System.Windows.Forms.dll");

            ScriptContent =
                "using System;" + Environment.NewLine +
                "using System.Collections.Generic; " + Environment.NewLine +
                "using System.Linq; " + Environment.NewLine +
                "using System.Text; " + Environment.NewLine +
                "using System.Threading.Tasks; " + Environment.NewLine +
                "using System.Windows.Forms; " + Environment.NewLine +
                "" + Environment.NewLine +
                "public class ScriptedClass" + Environment.NewLine +
                "{" + Environment.NewLine +
                "    public static void Execute()" + Environment.NewLine +
                "    {" + Environment.NewLine +
                "        MessageBox.Show(\"Hello World! from script\");" + Environment.NewLine +
                "    }" + Environment.NewLine +
                "}" + Environment.NewLine;
        }
        public CSharpScriptFile2()
        {
            ReferencedAssemblies.Add("Microsoft.CSharp.dll");
            ReferencedAssemblies.Add("System.dll");
            ReferencedAssemblies.Add("System.Core.dll");
            ReferencedAssemblies.Add("System.Data.dll");
            ReferencedAssemblies.Add("System.Linq.dll");
            ReferencedAssemblies.Add("System.Windows.Forms.dll");

            ScriptContent =
                "using System;" + Environment.NewLine +
                "using System.Collections.Generic; " + Environment.NewLine +
                "using System.Linq; " + Environment.NewLine +
                "using System.Text; " + Environment.NewLine +
                "using System.Threading.Tasks; " + Environment.NewLine +
                "using System.Windows.Forms; " + Environment.NewLine +
                "" + Environment.NewLine +
                "public class ScriptedClass" + Environment.NewLine +
                "{" + Environment.NewLine +
                "    public static object Execute(object obj)" + Environment.NewLine +
                "    {" + Environment.NewLine +
                "        return obj;" + Environment.NewLine +
                "    }" + Environment.NewLine +
                "}" + Environment.NewLine;
        }
예제 #4
0
        /// <summary>
        /// 构造函数
        /// </summary>
        public ViewEngineCompilationOptions()
        {
            ReferencedAssemblies = DefaultReferencedAssemblies();
            MetadataReferences   = new HashSet <MetadataReference>();
            TemplateNamespace    = "TemplateNamespace";
            Inherits             = "ViewEngineCore.ViewEngineTemplateBase";
            DefaultUsings        = new HashSet <string>()
            {
                "System.Linq"
            };

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                if (RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework", StringComparison.OrdinalIgnoreCase))
                {
                    ReferencedAssemblies.Add(
                        Assembly.Load(
                            new AssemblyName(
                                "netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51")));
                }
                else
                {
                    ReferencedAssemblies.Add(Assembly.Load(new AssemblyName("netstandard")));
                }
            }
        }
예제 #5
0
 public static void AddAssemblies(params string[] assemblies)
 {
     foreach (string item in assemblies)
     {
         if (!ReferencedAssemblies.Contains(item))
         {
             ReferencedAssemblies.Add(item);
         }
     }
 }
 // we need to use CompileEngine (which is slower but supports multiple file compilation
 public void compileExtraSourceCodeReferencesAndUpdateReferencedAssemblies()
 {
     if (ExtraSourceCodeFilesToCompile.size() > 0)
     {
         var assembly = new CompileEngine().compileSourceFiles(ExtraSourceCodeFilesToCompile);
         if (assembly != null)
         {
             ReferencedAssemblies.Add(assembly.Location);
             generateDebugSymbols = true;                // if there are extra assemblies we can't generate the assembly in memory
         }
     }
 }
예제 #7
0
 // we need to use CompileEngine (which is slower but supports multiple file compilation
 public void compileExtraSourceCodeReferencesAndUpdateReferencedAssemblies()
 {
     if (ExtraSourceCodeFilesToCompile.size() > 0)
     {
         "[CSharp Compiler] Compiling provided {0} external source code references".info(ExtraSourceCodeFilesToCompile.size());
         var assembly = new CompileEngine(UseCachedAssemblyIfAvailable).compileSourceFiles(ExtraSourceCodeFilesToCompile);
         if (assembly != null)
         {
             ReferencedAssemblies.Add(assembly.Location);
             CompileEngine.setCachedCompiledAssembly(ExtraSourceCodeFilesToCompile, assembly);
             generateDebugSymbols = true;                // if there are extra assemblies we can't generate the assembly in memory
         }
     }
 }
예제 #8
0
        private void RecordReferencedNamespaceAndAssembly(string nspace, string assembly)
        {
            if (!String.IsNullOrEmpty((nspace)))
            {
                if (!ReferencedNamespaces.Contains(nspace) && ReferencedNamespace.Name != nspace)
                {
                    ReferencedNamespaces.Add(nspace);
                }
            }

            if (!ReferencedAssemblies.Contains(assembly) && grainAssembly.GetName().Name + "Client" != assembly)
            {
                ReferencedAssemblies.Add(assembly);
            }
        }
예제 #9
0
    //Execute
    public static void Execute(string sourceCode, string className, string methodName)
    {
        var compParms = new CompilerParameters {
            // True - exe file generation, false - dll file generation
            GenerateExecutable = true,
            // True - memory generation, false - external file generation
            GenerateInMemory = true,
            //OutputAssembly = exeName,
            TreatWarningsAsErrors = false,
            ReferencedAssemblies.Add("System");
        };

        //Add references
        var assemblies = AppDomain.CurrentDomain
                         .GetAssemblies()
                         .Where(a => !a.IsDynamic)
                         .Select(a => a.Location);

        compParms.ReferencedAssemblies.AddRange(assemblies.ToArray());

        //compile
        var             csProvider      = new CSharpCodeProvider();
        CompilerResults compilerResults = csProvider.CompileAssemblyFromSource(compParms, sourceCode);

        if (compilerResults.Errors.Count > 0)
        {
            // Display compilation errors.
            Console.WriteLine("Errors building {0} into {1}",
                              className, compilerResults.PathToAssembly);
            foreach (CompilerError ce in compilerResults.Errors)
            {
                Console.WriteLine("  {0}", ce.ToString());
                Console.WriteLine();
            }
        }
        else
        {
            // Display a successful compilation message.
            Console.WriteLine("Source {0} built into {1} successfully.",
                              className, compilerResults.PathToAssembly);
        }

        //Execute
        object     typeInstance = compilerResults.CompiledAssembly.CreateInstance(className);
        MethodInfo mi           = typeInstance.GetType().GetMethod(methodName);

        mi.Invoke(typeInstance, null);
    }
예제 #10
0
 public XCodeCompileUnit(CodeCompileUnit source) : base()
 {
     if (source != null)
     {
         Namespaces.AddRange(source.Namespaces);
         EndDirectives.AddRange(source.EndDirectives);
         StartDirectives.AddRange(source.StartDirectives);
         AssemblyCustomAttributes.AddRange(source.AssemblyCustomAttributes);
         foreach (string name in source.ReferencedAssemblies)
         {
             ReferencedAssemblies.Add(name);
         }
         foreach (DictionaryEntry item in source.UserData)
         {
             UserData.Add(item.Key, item.Value);
         }
     }
 }
        /// <summary>
        ///     Initializes a new instance of the <see cref="RuleFile" /> class.
        /// </summary>
        public RuleFile()
        {
            // Add the Rule Engine and all the other stuff ...
            ReferencedAssemblies.Add("Microsoft.CSharp.dll");
            ReferencedAssemblies.Add("System.dll");
            ReferencedAssemblies.Add("System.Core.dll");
            ReferencedAssemblies.Add("System.Data.dll");
            ReferencedAssemblies.Add("System.Linq.dll");

            ReferencedAssemblies.Add("NRules.dll");

            ScriptContent =
                @"
                using NRules.Fluent.Dsl;

                public class PreferredCustomerDiscountRule : Rule
                {
                    public override void Define()
                    {
                        Customer customer = null;
                        IEnumerable<Order> orders = null;

                        When()
                            .Match<Customer>(() => customer, c => c.IsPreferred)
                            .Query(() => orders, x => x
                                .Match<Order>(
                                    o => o.Customer == customer,
                                    o => !o.IsDiscounted)
                                .Collect()
                                .Where(c => c.Any()));

                        Then()
                            .Do(ctx => orders.ToList().ForEach(o => o.ApplyDiscount(10.0)))
                            .Do(ctx => orders.ToList().ForEach(ctx.Update));
                    }
                }
                ";
        }
예제 #12
0
        //public string ProxyServer
        //{
        //    get { return m_proxyServer; }
        //    set { m_proxyServer = value; }
        //}

        #endregion

        #region utility
        protected override void AddReferencedAssemblies()
        {
            if (!ReferencedAssemblies.Contains("System.dll"))
            {
                ReferencedAssemblies.Add("System.dll");
            }
            if (!ReferencedAssemblies.Contains("mscorlib.dll"))
            {
                ReferencedAssemblies.Add("mscorlib.dll");
            }
            if (!ReferencedAssemblies.Contains("System.Xml.dll"))
            {
                ReferencedAssemblies.Add("System.Xml.dll");
            }
            if (!ReferencedAssemblies.Contains("System.Web.Services.dll"))
            {
                ReferencedAssemblies.Add("System.Web.Services.dll");
            }
            if (!ReferencedAssemblies.Contains("System.Data.dll"))
            {
                ReferencedAssemblies.Add("System.Data.dll");
            }
        }
예제 #13
0
 /// <summary>
 /// Add assembly references to the list of assemblies to
 /// include when compiling the code
 /// </summary>
 protected override void AddReferencedAssemblies()
 {
     if (!ReferencedAssemblies.Contains("System.dll"))
     {
         ReferencedAssemblies.Add("System.dll");
     }
     if (!ReferencedAssemblies.Contains("mscorlib.dll"))
     {
         ReferencedAssemblies.Add("mscorlib.dll");
     }
     if (!ReferencedAssemblies.Contains("system.xml.dll"))
     {
         ReferencedAssemblies.Add("system.xml.dll");
     }
     if (!ReferencedAssemblies.Contains(Assembly.GetExecutingAssembly().Location))
     {
         ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
     }
     if (!ReferencedAssemblies.Contains("System.Drawing.dll"))
     {
         ReferencedAssemblies.Add("System.Drawing.dll");
     }
 }
예제 #14
0
 /// <summary>
 /// Adds an assembly to the referenced assemblies from an existing
 /// .NET Type.
 /// </summary>
 /// <param name="instance"></param>
 public void AddAssemblyFromType(Type instance)
 {
     ReferencedAssemblies.Add(instance.Assembly.Location);
 }
예제 #15
0
        public MetadataAssembly(Assembly assembly, ImmutableArray <byte> assemblyBytes, ImmutableArray <IAssembly> dependencies)
        {
            _diagnostics   = new DiagnosticBag(this);
            _assembly      = assembly;
            _assemblyBytes = assemblyBytes;
            if (assembly.FullName is null)
            {
                _diagnostics.Add(new Diagnostic(
                                     new Location(),
                                     ErrorCode.InvalidMetadataAssembly,
                                     ImmutableArray.Create <object?>("Metadata Assembly has no name")));
            }

            var assemblyNameAttributes = assembly.GetAttributes <AssemblyNameAttribute>();

            if (assemblyNameAttributes.Length != 1)
            {
                _diagnostics.Add(new Diagnostic(
                                     new Location(),
                                     ErrorCode.InvalidMetadataAssembly,
                                     ImmutableArray.Create <object?>($"Metadata Assembly must have exactly one {nameof(AssemblyNameAttribute)}")));
                Name = new QualifiedName("");
            }
            else
            {
                Name = QualifiedName.Parse(assemblyNameAttributes[0].Name);
            }

            var assemblyFileVersionAttributes = assembly.GetAttributes <AssemblyFileVersionAttribute>();

            if (assemblyFileVersionAttributes.Length != 1)
            {
                _diagnostics.Add(new Diagnostic(
                                     new Location(),
                                     ErrorCode.InvalidMetadataAssembly,
                                     ImmutableArray.Create <object?>($"Metadata Assembly must have exactly one {nameof(AssemblyFileVersionAttribute)}")));
                Version = new Version(0, 0, 0);
            }
            else
            {
                var versionAttribute = assemblyFileVersionAttributes[0];
                if (!Version.TryParse(versionAttribute.Version, out var version))
                {
                    _diagnostics.Add(new Diagnostic(
                                         new Location(),
                                         ErrorCode.InvalidMetadataAssembly,
                                         ImmutableArray.Create <object?>($"Version `{version}` is invalid")));
                    Version = new Version(0, 0, 0);
                }
                else
                {
                    Version = version;
                }
            }

            _referencedAssemblies = new Lazy <ImmutableArray <IAssembly> >(
                () => ((IAssembly)this).CalculateReferencedAssemblies(dependencies, _diagnostics).ToImmutableArray());
            _referencedAssembliesAndSelf = new Lazy <ImmutableArray <IAssembly> >(
                () => ReferencedAssemblies.Add(this));

            _methodsByName    = new Lazy <IReadOnlyDictionary <QualifiedName, IMethod> >(GenerateMethods);
            _methods          = new Lazy <ImmutableArray <IMethod> >(() => _methodsByName.Value.Values.ToImmutableArray());
            _interfacesByName = new Lazy <IReadOnlyDictionary <QualifiedName, IInterface> >(GenerateInterfaces);
            _interfaces       = new Lazy <ImmutableArray <IInterface> >(() => _interfacesByName.Value.Values.ToImmutableArray());
            _allDiagnostics   = new Lazy <ImmutableArray <Diagnostic> >(() =>
            {
                _diagnostics.EnsureAllDiagnosticsCollectedForSymbol();
                return(_diagnostics.ToImmutableArray());
            });
        }