从C#程序关闭计算机的最佳方法是什么?
我发现了一些有效的方法 - 我会在下面发布它们 - 但它们都不是很优雅.我正在寻找更简单和原生的东西.net.
有没有办法列出CVS中两个标签之间已更改的所有文件?
每次我们发布版本时,我们都会将标记应用于该版本中的所有文件.我想找到版本之间发生变化的所有文件.
如果我能找到两个日期之间发生变化的所有文件,它也会起作用.
我有一个继承自Collection的简单类,并添加了几个属性.我需要将此类序列化为XML,但XMLSerializer忽略了我的其他属性.
我假设这是因为XMLSerializer提供ICollection和IEnumerable对象的特殊处理.最好的方法是什么?
这是一些示例代码:
using System.Collections.ObjectModel;
using System.IO;
using System.Xml.Serialization;
namespace SerialiseCollection
{
class Program
{
static void Main(string[] args)
{
var c = new MyCollection();
c.Add("Hello");
c.Add("Goodbye");
var serializer = new XmlSerializer(typeof(MyCollection));
using (var writer = new StreamWriter("test.xml"))
serializer.Serialize(writer, c);
}
}
[XmlRoot("MyCollection")]
public class MyCollection : Collection<string>
{
[XmlAttribute()]
public string MyAttribute { get; set; }
public MyCollection()
{
this.MyAttribute = "SerializeThis";
}
}
}
Run Code Online (Sandbox Code Playgroud)
这将输出以下XML(注意MyCollection元素中缺少MyAttribute):
<?xml version="1.0" encoding="utf-8"?>
<MyCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>Hello</string>
<string>Goodbye</string>
</MyCollection>
Run Code Online (Sandbox Code Playgroud)
我想要的是
<MyCollection …Run Code Online (Sandbox Code Playgroud) 我需要从Python中的子元素中获取属性值列表.
用一个例子来解释是最容易的.
给出一些像这样的XML:
<elements>
<parent name="CategoryA">
<child value="a1"/>
<child value="a2"/>
<child value="a3"/>
</parent>
<parent name="CategoryB">
<child value="b1"/>
<child value="b2"/>
<child value="b3"/>
</parent>
</elements>
Run Code Online (Sandbox Code Playgroud)
我希望能够做到这样的事情:
>>> getValues("CategoryA")
['a1', 'a2', 'a3']
>>> getValues("CategoryB")
['b1', 'b2', 'b3']
Run Code Online (Sandbox Code Playgroud)
它看起来像XPath的工作,但我对所有建议持开放态度.我也想听听你最喜欢的Python XML库.
我的目标是将一些Python 3代码作为Azure函数运行,但我无法使Python 3工作(我发现Azure函数中的python支持仍然是实验性的).
这是我尝试过的.
创建一个新的功能应用程序 - 我给它一个名字,并将其他所有内容保留为默认值.
使用以下代码创建了一个Python HttpTrigger函数:
import sys
import os
response = open(os.environ['res'], 'w')
response.write(sys.version)
response.close()
Run Code Online (Sandbox Code Playgroud)运行此函数会得到"2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)]"预期的输出,因为2.7.8是Azure Functions环境中安装的python的默认版本.
当我再次运行该功能时,我得到输出"3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)]"- 所以一切都很好.
但是,如果我重新启动功能应用程序(或者只是暂时离开它以便关闭它),那么一切都会中断.运行该功能给出:
{
"id": "dd7c4462-0d73-49e0-8779-67b15a9bba82",
"requestId": "ff553805-501d-4ea6-93f6-7bd6fa445a37",
"statusCode": 500,
"errorCode": 0,
"message": "Exception while executing function: Functions.HttpTriggerPython31 -> "
}
Run Code Online (Sandbox Code Playgroud)
日志显示:
2017-11-09T17:37:04.988 Function started (Id=941e5bef-e5e0-4604-8533-dd2a5fcaddf0)
2017-11-09T17:37:05.348 Exception while executing …Run Code Online (Sandbox Code Playgroud) 在我的代码中,有几个字符串用作访问资源的密钥.这些密钥具有特定格式,例如
string key = "ABC123";
Run Code Online (Sandbox Code Playgroud)
目前,所有这些键都存储为字符串,但我想使事情更健壮和类型安全.理想情况下,我想在编译时检查字符串是否格式正确.
下一步是创建一个从字符串初始化的ResourceKey类.然后,我可以在运行时检查字符串的格式,例如
ResourceKey key = "ABC123";
Run Code Online (Sandbox Code Playgroud)
其中ResourceKey定义为:
using System.Diagnostics;
using System.Text.RegularExpressions;
class ResourceKey
{
public string Key { get; set; }
public static implicit operator ResourceKey (string s)
{
Debug.Assert(Regex.IsMatch(s, @"^[A-Z]{3}[0-9]{3}$"));
return new ResourceKey () { Key = s };
}
}
Run Code Online (Sandbox Code Playgroud)
我真正想做的是拥有一种编译时断言,以便在任何人试图使用无效密钥时程序无法构建.例如
ResourceKey k1 = "ABC123"; // compiles
ResourceKey k2 = "DEF456"; // compiles
ResourceKey k3 = "hello world"; // error at compile time
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这个目标?
谢谢