HearthBuddy CSharpCodeProvider

 Source

Source 1

namespace Hearthbuddy.Windows
{
    // Token: 0x02000041 RID: 65
    public class MainWindow : Window, IComponentConnector

// Hearthbuddy.Windows.MainWindow
// Token: 0x0600021B RID: 539 RVA: 0x0008A250 File Offset: 0x00088450
private static void smethod_1(string string_0, string string_1, IEnumerable<string> ienumerable_0)
{
    try
    {
        using (CSharpCodeProvider csharpCodeProvider = new CSharpCodeProvider())
        {
            CompilerParameters compilerParameters = new CompilerParameters
            {
                GenerateExecutable = false,
                GenerateInMemory = true
            };
            foreach (string value in ienumerable_0)
            {
                compilerParameters.ReferencedAssemblies.Add(value);
            }
            CompilerResults compilerResults = csharpCodeProvider.CompileAssemblyFromSource(compilerParameters, new string[]
            {
                string_1
            });
            if (compilerResults.Errors.Count > 0)
            {
                StringBuilder stringBuilder = new StringBuilder();
                foreach (object obj in compilerResults.Errors)
                {
                    CompilerError compilerError = (CompilerError)obj;
                    stringBuilder.AppendFormat(string.Concat(new object[]
                    {
                        "Line number ",
                        compilerError.Line,
                        ", Error Number: ",
                        compilerError.ErrorNumber,
                        ", '",
                        compilerError.ErrorText,
                        ";"
                    }), Array.Empty<object>());
                    stringBuilder.AppendLine();
                }
                throw new Exception(stringBuilder.ToString());
            }
            Type type = compilerResults.CompiledAssembly.GetType(string_0);
            object obj2 = Activator.CreateInstance(type);
            object obj3 = type.GetMethod("Execute").Invoke(obj2, new object[0]);
            if (obj3 != null)
            {
                MainWindow.ilog_0.Info(obj3);
            }
        }
    }
    catch (Exception exception)
    {
        MainWindow.ilog_0.Error("[Ui] An exception occurred:", exception);
    }
}

Source 2

Hearthbuddy\Triton\Common\CodeCompiler.cs 

public CompilerResults Compile()
        {
            this.method_2();
            this.method_3();
            if (this.SourceFilePaths.Count != 0)
            {
                CompilerResults result;
                using (CSharpCodeProvider csharpCodeProvider = new CSharpCodeProvider(new Dictionary<string, string>
                {
                    {
                        "CompilerVersion",
                        string.Format(CultureInfo.InvariantCulture.NumberFormat, "v{0:N1}", this.CompilerVersion)
                    }
                }))
                {
                    csharpCodeProvider.Supports(GeneratorSupport.Resources);
                    if (this.resourceWriter_0 != null)
                    {
                        this.resourceWriter_0.Close();
                        this.resourceWriter_0.Dispose();
                        this.resourceWriter_0 = null;
                    }
                    foreach (Stream stream in this.list_2)
                    {
                        try
                        {
                            stream.Close();
                            stream.Dispose();
                        }
                        catch
                        {
                        }
                    }
                    this.list_2.Clear();
                    CompilerResults compilerResults = csharpCodeProvider.CompileAssemblyFromFile(this.Options, this.SourceFilePaths.ToArray());
                    if (!compilerResults.Errors.HasErrors)
                    {
                        this.CompiledAssembly = compilerResults.CompiledAssembly;
                    }
                    compilerResults.TempFiles.Delete();
                    foreach (string path in this.list_1)
                    {
                        try
                        {
                            File.Delete(path);
                        }
                        catch
                        {
                        }
                    }
                    this.list_1.Clear();
                    result = compilerResults;
                }
                return result;
            }
            if (this.resourceWriter_0 != null)
            {
                this.resourceWriter_0.Close();
                this.resourceWriter_0.Dispose();
                this.resourceWriter_0 = null;
            }
            foreach (Stream stream2 in this.list_2)
            {
                try
                {
                    stream2.Close();
                    stream2.Dispose();
                }
                catch
                {
                }
            }
            this.list_2.Clear();
            foreach (string path2 in this.list_1)
            {
                try
                {
                    File.Delete(path2);
                }
                catch
                {
                }
            }
            this.list_1.Clear();
            return null;
        }

 

 

problem

The following code will not compile

using System;
using System.Linq;
using System.Xml.Linq;

namespace HREngine.Bots
{
    public class XmlHelper
    {
        private static XElement _cardDatabase;

        public static string GetCardNameByCardId(string filePath, string cardId)
        {
            if (_cardDatabase == null)
            {
                _cardDatabase = XElement.Load(filePath);
            }

            var tempTargetElement = _cardDatabase.Elements("Entity")
                .FirstOrDefault(x => x.Attribute("CardID")?.Value == cardId);
            var tempTargetElement2 = tempTargetElement?.Elements("Tag")
                .FirstOrDefault(x => x.Attribute("name")?.Value == "CARDNAME");
            XElement targetElement = tempTargetElement2?.Element("enUS");
            if (targetElement == null)
            {
                throw new Exception(string.Format("Can not find card by Id {0}", cardId));
            }

            return targetElement.Value;
        }
    }
}

 

"The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?)"

The type or namespace name 'Xml' does not exist in the namespace 'System' (are you missing an assembly reference?)

The type or namespace name 'XElement' could not be found (are you missing a using directive or an assembly reference?)

 

2019-08-04 18:06:56,506 [7] ERROR AssemblyLoader`1 (null) - [Reload] An exception occurred.
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\clu\Desktop\GitHub\HearthbuddyRelease\bin\roslyn\csc.exe'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at Microsoft.CodeDom.Providers.DotNetCompilerPlatform.Compiler.get_CompilerName()
at Microsoft.CodeDom.Providers.DotNetCompilerPlatform.Compiler.FromFileBatch(CompilerParameters options, String[] fileNames)
at Microsoft.CodeDom.Providers.DotNetCompilerPlatform.Compiler.CompileAssemblyFromFileBatch(CompilerParameters options, String[] fileNames)
at System.CodeDom.Compiler.CodeDomProvider.CompileAssemblyFromFile(CompilerParameters options, String[] fileNames)
at Triton.Common.CodeCompiler.Compile()
at Triton.Common.AssemblyLoader`1.Reload(String reason)

Guess you like

Origin www.cnblogs.com/chucklu/p/11300734.html