我读到你无法使用CSharpCodeProvider编译C#6.0,因此尝试使用Roslyn.但我找不到一个很好的例子,如何加载文件,然后将其编译为DLL.
我应该如何使用Roslyn编写与此代码类似的内容?或者还有其他方法吗?现在,当我尝试编译包含对带有C#6.0代码的项目的引用的文件时,它只是说"命名空间'y'中不存在类型或命名空间名称'x'(您是否缺少程序集引用?)"
public string CompileCode()
{
var provider = new CSharpCodeProvider();
var outputPath = Path.Combine(Path.GetDirectoryName(_path), $"Code.dll");
var compilerparams = new CompilerParameters(_referencedAssemblies, outputPath);
CompilerResults results = provider.CompileAssemblyFromFile(compilerparams, _path);
var dllPath = results.PathToAssembly;
if (!results.Errors.HasErrors)
return dllPath;
PrintError(results.Errors);
return "";
}
Run Code Online (Sandbox Code Playgroud)
总之,我想:
现在我正在使用uiautomator像这样转储UI:
adb shell uiautomator dump
Run Code Online (Sandbox Code Playgroud)
它工作正常,但执行它需要大约3秒钟.所以我想知道是否有更快的方法呢?就像创建一个转储UI的服务一样,还是需要一段时间?
我知道如何在方法内部创建局部变量,例如:
LocalDeclarationStatement(VariableDeclaration(IdentifierName("MyClass"))
.WithVariables(SingletonSeparatedList(VariableDeclarator(Identifier("nameOfvariable"))
.WithInitializer(
EqualsValueClause(
ObjectCreationExpression(IdentifierName("MyClass")).WithArgumentList(arguments)
.WithNewKeyword(Token(SyntaxKind.NewKeyword)))))));
Run Code Online (Sandbox Code Playgroud)
会给我:
MyClass nameOfvariable = new MyClass();
Run Code Online (Sandbox Code Playgroud)
但是说我已经创建了一个字段,现在我只想像这样初始化它(在方法,构造函数或任何东西中):
nameOfVariable = new MyClass();
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?我猜这与VariableDeclerator有关,但是我找不到正确的方法,因此可以将其添加到包含StatementSyntaxes的列表中。我也可以将VariableDecleration更改为“ VariableDeclaration(IdentifierName(”“))”“,但这在语句的前面给了我一个丑陋的额外空间。
似乎我在与Roslyn的一些非常基础的东西斗争,我尝试检查http://roslynquoter.azurewebsites.net/,但这听起来像是强制的方式(感觉它创建了很多不必要的代码)。
更新:应该澄清一下,我知道如何创建方法/构造函数。当我只能访问字段名称和字段类型时,我只是在寻找一种初始化字段的方法。因此,我要生成的唯一代码是:
myField = new MyField();
Run Code Online (Sandbox Code Playgroud) 我有一个简单的服务配对蓝牙设备,它看起来像这样:
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
if(!extras.containsKey("bluetoothAddress"))
return;
String bluetoothAddress = extras.getString("bluetoothAddress");
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if(!adapter.isEnabled()) {
adapter.enable();
}
BluetoothDevice device = adapter.getRemoteDevice(bluetoothAddress);
device.createBond();
}
Run Code Online (Sandbox Code Playgroud)
它工作得很好,除了有时会弹出对对话框,有时它会显示在我的通知栏中,我必须手动打开它.有没有办法确保它总是弹到前面?
我试图谷歌上面,我唯一能找到的是,如果你留在蓝牙设置,它总是弹出,但这似乎是一个丑陋的解决方案.所有这一切的原因是我正在使用自动化,并希望确保当我运行我的服务时,我得到对对话只需单击"配对".
我正在尝试使用 adb 将文本写入文件并偶然发现一些问题。
如果我分两步进行,如下所示:
adb shell
echo "hello" > /sdcard/temp/hello.txt
Run Code Online (Sandbox Code Playgroud)
有用。但如果我尝试像这样在一行上执行此操作:
adb shell echo "hello" > /sdcard/temp/hello.txt
Run Code Online (Sandbox Code Playgroud)
我得到“没有这样的文件或目录”。我必须以某种方式改变路径吗?我将从另一个脚本中调用它,我必须将它放在一行上。
我正在尝试创建一个在默认图像查看器中打开图像的简短方法,并在time几毫秒后关闭它.
现在它看起来像这样:
public static async void OpenImage(string path, int time)
{
var process = new Process();
process.StartInfo.FileName = path;
process.Start();
await Task.Delay(time);
process.Kill()
process.Close()
}
Run Code Online (Sandbox Code Playgroud)
我可以看到图像,但随后process.Kill()抛出InvalidOperationException"没有进程与此对象相关联".
我错过了什么或者还有其他办法吗?
更新:
现在也测试了这个:
public static async void OpenImage(string path, int time)
{
var process = Process.Start(path)
await Task.Delay(time);
process.Kill()
process.Close()
}
Run Code Online (Sandbox Code Playgroud)
但后来Process.Start()回归null.所以也许我必须.exe直接打电话给faljbour评论?