假设我有以下内容enum:
public enum Colors
{
White = 10,
Black = 20,
Red = 30,
Blue = 40
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有办法遍历所有成员Colors以查找成员名称及其值.
我想在我的Python代码中使用一个模块,例如BeautifulSoup,所以我通常将它添加到文件的顶部:
from BeautifulSoup import BeautifulSoup
Run Code Online (Sandbox Code Playgroud)
但是,当我分发我正在编写的模块时,其他人可能没有BeautifulSoup,所以我只是将它包含在我的目录结构中,如下所示:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 9/19/2011 5:45 PM BeautifulSoup
-a--- 9/17/2011 8:06 PM 4212 myscript.py
Run Code Online (Sandbox Code Playgroud)
现在,我修改的myscript.py文件在顶部看起来像这样引用BeautifulSoup的本地副本:
from BeautifulSoup.BeautifulSoup import BeautifulSoup, CData
Run Code Online (Sandbox Code Playgroud)
但是如果使用我的库的开发人员已经在他们的机器上安装了BeautifulSoup呢?我想修改myscript.py,以便检查是否已安装BeautifulSoup,如果已安装,请使用标准模块.否则,请使用附带的一个.
使用Pseudo-python:
if fBeautifulSoupIsInstalled:
from BeautifulSoup import BeautifulSoup, CData
else:
from BeautifulSoup.BeautifulSoup import BeautifulSoup, CData
Run Code Online (Sandbox Code Playgroud)
这可能吗?如果是这样,怎么样?
我需要在C#代码或XML文档中做什么才能让XDocument解析器为Values的XElements 读取文字空格?
我有一个XML文档,其中一部分如下所示:
<NewLineString> </NewLineString>
<IndentString> </IndentString>
Run Code Online (Sandbox Code Playgroud)
我正在XELement使用LINQ查询将每个值添加到数据字典中; 该.ForEach部分看起来是这样的:
.ForEach(x => SchemaDictionary.Add(
LogicHelper.GetEnumValue(x.Name.ToString()), x.Value));
Run Code Online (Sandbox Code Playgroud)
为了测试是否保留了空白值,我打印出一行数据字典中每个值项的字符数.在下面的代码,x表示KeyValuePair与Aggregate简单地使字符整数值的字符串:
x.Value.ToCharArray()
.Aggregate<char,string>("",(word,c) => word + ((int)c).ToString() + " " )
));
Run Code Online (Sandbox Code Playgroud)
我期望看到10 13的<NewLineString>价值,32 32 32 32对于<IndentString>价值.但是,没有为每个值打印任何内容(注意:XML中的其他转义值,例如<正确打印其字符数).
我需要在C#代码或XML文档中做什么才能使我的解析器将完整的空白字符串添加到数据字典中?
我在Visual Studio Express中有一个如下所示的解决方案:

LogicSchemaC#中的类,它将在运行时解析指定的XML文件.以下是main方法Program.cs:
LogicSchema ls = new LogicSchema(
XDocument.Load(
"schemas\\C#Schema.xml",
LoadOptions.PreserveWhitespace));
Run Code Online (Sandbox Code Playgroud)
我在我的解决方案中创建了一个名为"schemas"的文件夹,用于保存XML文件并将Build Action设置为Content,并将"Copy to Output Directory"值设置为Copy for new.
我的期望是,如果在记事本中打开文件,进行更改并保存,当我按F5时,XML文件的更新版本将被复制到输出目录(在本例中为bin\debug).但是,除非我选择Rebuild,否则更新的文件不会复制到输出目录.常规构建不起作用.
我需要更改什么,以便在按F5时,文件在更新时被复制到输出目录中?
我正在设置一个简单的帮助器类来保存我正在解析的文件中的一些数据.属性的名称与我期望在文件中找到的值的名称相匹配.我想添加一个调用AddPropertyValue到我的类的方法,这样我就可以为属性赋值而无需通过名称显式调用它.
该方法如下所示:
//C#
public void AddPropertyValue(string propertyName, string propertyValue) {
//code to assign the property value based on propertyName
}
---
'VB.NET'
Public Sub AddPropertyValue(ByVal propertyName As String, _
ByVal propertyValue As String)
'code to assign the property value based on propertyName '
End Sub
Run Code Online (Sandbox Code Playgroud)
实现可能如下所示:
C#/ VB.NET
MyHelperClass.AddPropertyValue("LocationID","5")
Run Code Online (Sandbox Code Playgroud)
这是否可能无需针对提供的每个单独的属性名称进行测试propertyName?
也许我不知道for循环索引变量是如何得到作用域的,但是当我的一个循环没有完成时我感到非常惊讶,似乎因为在循环内调用的函数也包含了一个ifor for循环索引.
这是我用一个小脚本来演示这种行为:
var loopOne = function(test) {
for(i = 0; i < test.length; i++)
console.log(getMask(test));
};
var getMask = function(pass) {
var s = "";
for (i = 0; i < pass.length; i++) {
s = s + "*";
}
return s;
};
loopOne('hello');
Run Code Online (Sandbox Code Playgroud)
如果我在Chrome中运行它并查看控制台日志,我应该看到*****五次.但是,我只看过一次.经过进一步检查,如果我输入iChrome javascript控制台,它将输出6(= 'hello'.length + 1).这使我认为它i已成为全球范围的一部分,并不仅限于for需要它的循环范围.
它是否正确?如果是这样,for在javascript中定义循环的索引变量有什么更好的做法?
我考虑在C#/ .NET中编写一个应用程序,它将以编程方式更改用户计算机上的XML文件,并尝试确定Silverlight(以及哪些版本,以何种模式)将适用于此任务.
这是工作流程:
这在Silverlight中是否可行?如果是这样,在哪个版本和什么条件下?
如果我转到 Visual Studio 并键入MyEnumerable.,我将看到可以从该对象执行的所有方法的列表。这个列表也显示了扩展方法,这很好,但是有没有办法(键盘快捷方式?)缩小这个列表以不包括扩展方法?
我正在使用一个属性来装饰一个属性的setter,这个属性被TestMaxStringLength称为在setter中调用的方法中用于验证.
该物业目前看起来像这样:
public string CompanyName
{
get
{
return this._CompanyName;
}
[TestMaxStringLength(50)]
set
{
this.ValidateProperty(value);
this._CompanyName = value;
}
}
Run Code Online (Sandbox Code Playgroud)
但我宁愿它看起来像这样:
[TestMaxStringLength(50)]
public string CompanyName
{
get
{
return this._CompanyName;
}
set
{
this.ValidateProperty(value);
this._CompanyName = value;
}
}
Run Code Online (Sandbox Code Playgroud)
ValidateProperty用于查找setter属性的代码是:
private void ValidateProperty(string value)
{
var attributes =
new StackTrace()
.GetFrame(1)
.GetMethod()
.GetCustomAttributes(typeof(TestMaxStringLength), true);
//Use the attributes to check the length, throw an exception, etc.
}
Run Code Online (Sandbox Code Playgroud)
如何更改ValidateProperty代码以在属性上查找属性而不是 …
如何确定字符串是否包含转义的unicode,以便您知道是否要运行.decode("unicode-escape")?
例如:
test.py
# -*- coding: utf-8 -*-
str_escaped = '"A\u0026B"'
str_unicode = '"?????? ? ????"'
arr_all_strings = [str_escaped, str_unicode]
def is_escaped_unicode(str):
#how do I determine if this is escaped unicode?
pass
for str in arr_all_strings:
if is_escaped_unicode(str):
str = str.decode("unicode-escape")
print str
Run Code Online (Sandbox Code Playgroud)
当前输出:
"A\u0026B"
"?????? ? ????"
Run Code Online (Sandbox Code Playgroud)
预期产量:
"A&B"
"?????? ? ????"
Run Code Online (Sandbox Code Playgroud)
我如何定义is_escaped_unicode(str)以确定传递的字符串是否实际上是转义unicode?
c# ×7
.net ×6
reflection ×3
file-io ×2
python ×2
attributes ×1
enumeration ×1
enums ×1
ide ×1
iteration ×1
javascript ×1
linq ×1
module ×1
python-2.x ×1
scope ×1
silverlight ×1
unicode ×1
vb.net ×1
whitespace ×1
xml ×1