源码

源码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 > )
{
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[]);
if (obj3 != null)
{
MainWindow.ilog_0.Info(obj3);
}
}
}
catch (Exception exception)
{
MainWindow.ilog_0.Error("[Ui] An exception occurred:", exception);
}
}

源码2

Hearthbuddy\Triton\Common\CodeCompiler.cs

public CompilerResults Compile()
{
this.method_2();
this.method_3();
if (this.SourceFilePaths.Count != )
{
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;
}

在CodeCompiler的构造函数中,调用了下面的方法。在编译之前,先删除CompiledAssemblies文件夹下已经编译好的文件。

// Triton.Common.CodeCompiler
// Token: 0x0600155E RID: 5470 RVA: 0x000CE98C File Offset: 0x000CCB8C
private static void smethod_1()
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "CompiledAssemblies");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
foreach (string path2 in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories))
{
try
{
File.Delete(path2);
}
catch (Exception)
{
}
}
foreach (string path3 in Directory.GetDirectories(path))
{
try
{
Directory.Delete(path3);
}
catch (Exception)
{
}
}
if (!Directory.Exists(CodeCompiler.CompiledAssemblyPath))
{
Directory.CreateDirectory(CodeCompiler.CompiledAssemblyPath);
}
}

问题

如下代码无法通过编译

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)

Reload的逻辑

// Triton.Common.AssemblyLoader<T>
// Token: 0x06001556 RID: 5462 RVA: 0x000CE6C0 File Offset: 0x000CC8C0
public void Reload(string reason)
{
this.ilog_0.Debug(string.Format("Reloading AssemblyLoader<{0}> - {1}", typeof(T), reason));
this.Instances = new List<T>();
if (!Directory.Exists(this.string_0))
{
this.ilog_0.Error(string.Format("Could not Reload assemblies because the path \"{0}\" does not exist.", this.string_0));
return;
}
foreach (string path in Directory.GetDirectories(this.string_0))
{
try
{
CodeCompiler codeCompiler = new CodeCompiler(path);
CompilerResults compilerResults = codeCompiler.Compile();
if (compilerResults != null)
{
if (compilerResults.Errors.HasErrors)
{
foreach (object obj in compilerResults.Errors)
{
this.ilog_0.Error("Compiler Error: " + obj.ToString());
}
}
else
{
this.Instances.AddRange(new TypeLoader<T>(codeCompiler.CompiledAssembly, null));
}
}
}
catch (Exception ex)
{
if (ex is ReflectionTypeLoadException)
{
foreach (Exception exception in (ex as ReflectionTypeLoadException).LoaderExceptions)
{
this.ilog_0.Error("[Reload] An exception occurred.", exception);
}
}
else
{
this.ilog_0.Error("[Reload] An exception occurred.", ex);
}
}
}
using (List<T>.Enumerator enumerator2 = new TypeLoader<T>(null, null).GetEnumerator())
{
while (enumerator2.MoveNext())
{
AssemblyLoader<T>.Class229 @class = new AssemblyLoader<T>.Class229();
@class.gparam_0 = enumerator2.Current;
if (!this.Instances.Any(new Func<T, bool>(@class.method_0)))
{
this.Instances.Add(@class.gparam_0);
}
}
}
if (this.eventHandler_0 != null)
{
this.eventHandler_0(this, null);
}
}

string_0的赋值

// Token: 0x06001551 RID: 5457 RVA: 0x000CE580 File Offset: 0x000CC780
public AssemblyLoader(string directory, bool detectFileChanges)
{
this.Instances = new List<T>();
this.string_0 = directory;
this.bool_0 = detectFileChanges;
if (this.bool_0)
{
this.fileSystemWatcher_0.Path = directory;
this.fileSystemWatcher_0.Filter = "*.cs";
this.fileSystemWatcher_0.IncludeSubdirectories = true;
this.fileSystemWatcher_0.EnableRaisingEvents = true;
this.fileSystemWatcher_0.Changed += this.method_0;
this.fileSystemWatcher_0.Created += this.method_1;
this.fileSystemWatcher_0.Deleted += this.method_2;
}
this.Reload("Initializing");
}

method2在读取文件路径

// Triton.Common.CodeCompiler
// Token: 0x06001573 RID: 5491 RVA: 0x000CF470 File Offset: 0x000CD670
private void method_2()
{
if (this.FileStructure == CodeCompiler.FileStructureType.Folder)
{
foreach (string string_ in Directory.GetFiles(this.SourcePath, "*.resx", SearchOption.AllDirectories))
{
this.method_1(string_);
}
bool flag = false;
foreach (string string_2 in Directory.GetFiles(this.SourcePath, "*.baml", SearchOption.AllDirectories))
{
flag = true;
this.method_0(string_2);
}
foreach (string text in Directory.GetFiles(this.SourcePath, "*.cs", SearchOption.AllDirectories))
{
if (text.ToLowerInvariant().Contains(".g.cs"))
{
if (flag)
{
this.SourceFilePaths.Add(text);
}
}
else if (text.ToLowerInvariant().Contains(".xaml.cs"))
{
if (flag)
{
this.SourceFilePaths.Add(text);
}
}
else
{
this.SourceFilePaths.Add(text);
}
}
return;
}
this.SourceFilePaths.Add(this.SourcePath);
}

HearthBuddy CSharpCodeProvider 如何编译cs文件的更多相关文章

  1. 用csc命令行手动编译cs文件

    一般初学c#时,用记事本写代码,然后用命令行执行csc命令行可以编译cs文件.方法有两种 1:配置环境,一劳永逸 一般来说在C:\Windows\Microsoft.NET\Framework\v4. ...

  2. C# 使用命令行编译单个CS文件

    编译单个CS文件. 1.编译   File.cs   以产生   File.exe:       csc   File.cs     2.编译   File.cs   以产生   File.dll:  ...

  3. 九、将cs文件快速的转换成可执行文件和响应文件(配置编译开关的文件)

    1.将包含多个类型的源代码文件转换为可以部署的文件.有如下Program.cs的文件,代码如下: public sealed class Program { public static void Ma ...

  4. C#.NET常见问题(FAQ)-如何将cs文件编译成dll文件 exe文件 如何调用dll文件

    比如我要把TestDLL.cs文件编译成dll文件,则在命令提示符下,输入下面的命令,生成的文件为TestDLL.dll csc /target:library TestDLL.cs 注意前提是你安装 ...

  5. VS.NET2013发布网站的时候去掉.cs文件(预编译)(转)

      在要发布的网站上右键,选择"发布网站".   在发布窗口中,会让你选择一个发布配置文件,没有的话点击下拉菜单在里面选择新建一个. NEXT.   好,现在发布一下网站.发布出来 ...

  6. C#.NET如何将cs文件编译成dll文件 exe文件 如何调用dll文件

    比如我要把TestDLL.cs文件编译成dll文件,则在命令提示符下,输入下面的命令,生成的文件为TestDLL.dll csc /target:library TestDLL.cs 注意前提是你安装 ...

  7. 如何把.cs文件编译成DLL文件

    开始--程序--Microsoft Visual Studio.NET 2013--Visual Studio.NET工具,点击其中的"VS2013 开发人员命令提示",就会进入M ...

  8. C# 调用WebService的3种方式 :直接调用、根据wsdl生成webservice的.cs文件及生成dll调用、动态调用

    1.直接调用 已知webservice路径,则可以直接 添加服务引用--高级--添加web引用 直接输入webservice URL.这个比较常见也很简单 即有完整的webservice文件目录如下图 ...

  9. 安利一个MVC的好东西,RazorGenerator.MsBuild,可以自动编译cshtml文件

    在传统的asp.net webForm 开发里,在发布时,如果选择预编译,就会自动将所有的aspx 文件编译,在发布后的目录里,就看不到aspx的源代码了,同时因为是预编译的,所以每个页面打开速度都挺 ...

随机推荐

  1. 使用css让表头固定的方法

    1.可以使用display: table; width: 100%; table-layout: fixed; table-layout: fixed;设置表格布局算法.tableLayout 属性用 ...

  2. echarts —— tooltip 鼠标悬浮显示提示框属性

    最近一直在使用echarts,当然也被其中的各种属性整的头大,记录一下其中遇到的问题. tooltip:鼠标悬浮时显示的提示框. 今天想要记录的是[自定义提示框的内容],如下图,鼠标悬浮时提示框内显示 ...

  3. Java基础加强-注解

    /*注解(Annotation)*/(注解相当于一个特殊的类,注解类@interface A) 了解注解及java提供的几个基本注解 1. @SuppressWarnings 指示应该在注释元素(以及 ...

  4. 13.MySQL锁机制

    锁的分类 从对数据的类型 (读\写)分: 1.读锁(共享锁):针对同一份数据,多个读操作可以同时进行而不会互相影响 2.写锁(排它锁):当前写操作没有完成前,它会阻断其他写锁和读锁 从对数据操作的粒度 ...

  5. es的相关知识二(检索文档)

    一.es的使用 1.检索文档: 想要从Elasticsearch中获取文档,我们使用同样的 _index  . _type  . _id  ,但是HTTP方法改为 GET  : GET /{index ...

  6. 【NOIP/CSP2019】D1T2 括号树

    原题: 因为是NOIP题,所以首先先看特殊数据,前35分是一条长度不超过2000的链,N^2枚举所有子区间暴力check就能拿到分 其次可以思考特殊情况,一条链的情况怎么做 OI系列赛事的特殊性质分很 ...

  7. HAL UART DMA 数据收发

    UART使用DMA进行数据收发,实现功能,串口2发送指令到上位机,上位机返回数据给串口2,串口2收到数据后由串口1进行转发,该功能为实验功能 1.UART与DMA通道进行绑定 void HAL_UAR ...

  8. python——flask常见接口开发(简单案例)

    python——flask常见接口开发(简单案例)原创 大蛇王 发布于2019-01-24 11:34:06 阅读数 5208 收藏展开 版本:python3.5+ 模块:flask 目标:开发一个只 ...

  9. 第四章 Jinja2模版

    模板简介: 在之前的章节中,视图函数只是直接返回文本,而在实际生产环境中的页面大多是带有样式和复杂逻辑的HTML代码,这可以让浏览器渲染出非常漂亮的页面.目前市面上有非常多的模板系统,其中最知名好用的 ...

  10. PHP流协议

    目前对PHP流协议的定义是数据传输过程中,不同数据类型采用不同处理函数的技术规范(个人理解)这一规范比起传统文件处理函数来的更规范(诸如fget,fwrite,fopen,fclose等) $opts ...