我有自己的自定义配置部分,但想创建一个内部具有简单键/值的新元素.现在我有一个工作版本,但似乎有很多代码用于这么简单的任务.是否有改进的做事方式?
下面是我的配置和自定义配置类的剥离版本.
web.config中
<myRootNode>
<myNode>
<add key="a" value="" />
<add key="b" value="" />
<add key="c" value="" />
...
</myNode>
...any other nodes
</myRootNode>
Run Code Online (Sandbox Code Playgroud)
自定义配置类
public class MyRootNode : ConfigurationSection
{
[ConfigurationProperty("myNode")]
public MyNodeElement MyNode
{
get { return (MyNodeElement)this["myNode"]; }
}
}
[ConfigurationCollection(typeof(BaseElement), AddItemName = "add")]
public class MyNodeElement : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new BaseElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((BaseElement)element).Key;
}
public BaseElement this[int index]
{
get { return this.BaseGet(index) as …Run Code Online (Sandbox Code Playgroud) 我想动态设置Number文本块的文化格式,其中包含传递给MyUserControl的文化和数字值.MyCulture和Number值将传递给MyCustomControl,其格式为"en-GB","en-US"等.
我在asp.NET MVC中使用扩展方法做了类似的事情,但需要帮助如何在WPF中将它拼凑在一起.
示例MVC扩展方法
public static MvcHtmlString CulturedAmount(this decimal value,
string format, string locale)
{
if (string.IsNullOrEmpty(locale))
locale = HttpContext.Current.Request.UserLanguages[0];
return MvcHtmlString.Create(value.ToString(format,
CultureInfo.CreateSpecificCulture(locale)));
}
Run Code Online (Sandbox Code Playgroud)
窗口
//MyMoney is a decimal, MyCulture is a string (e.g. "en-US")
<MyCustomControl Number="{Binding MyMoney}" Culture="{Binding MyCulture}"
Text="Some Text" />
Run Code Online (Sandbox Code Playgroud)
MyCustomControl
<StackPanel>
<TextBlock Text="{Binding Number, ElementName=BoxPanelElement,
StringFormat={}{0:C}}" /> //display this with specific culture
<TextBlock Text="{Binding Text, ElementName=BoxPanelElement}" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud) 我有三个项目,ProjectA(exe),ProjectB(exe)和ProjectD(类库)
项目A引用System.Data.OracleClient.dll和ProjectD.项目B仅参考ProjectD.安装了32位客户端版本的oracle,因此ProjectA必须是32位应用程序.项目B可以构建为64位应用程序.
项目A构建设置:
平台:活动(x86)
平台目标:x86
项目B构建设置:
平台:活动(任何CPU)
平台目标:任何CPU
我的问题是ProjectD(类库)的构建设置应该是什么,当ProjectA和ProjectB构建时,它是否以不同的方式构建ProjectD?在项目的沟通方面,对CLR的更深入解释将是很好的.
ProjectA和ProjectB将用于64位Windows Server 2008.无需安装,只需要独立的exe.
我正在尝试创建一个 linq 扩展方法,该方法返回选定节点的路径。
到节点 Item4 的路径将产生 -{ Item1, Item2, Item4 }
到节点 Item3 的路径将产生 -{ Item1, Item3 }
public class Item
{
public int Id { get; set; }
public IList<Item> Items { get; set; }
}
Item1
Item2
Item4
Item3
Run Code Online (Sandbox Code Playgroud)
调用代码
var path = myItem.Items
.Path(e => e.Items, e => MyPredicateCondition())
.ToList();
Run Code Online (Sandbox Code Playgroud)
扩展方法
public static IEnumerable<T> Path<T>(
this IEnumerable<T> source,
Func<T, IEnumerable<T>> childrenSelector,
Predicate<T> condition)
{
if (source == null || !source.Any()) return default(T);
var attempt = …Run Code Online (Sandbox Code Playgroud) 是否可以使用您想要插入的字符串格式的变量.
public class Setting
{
public string Format { get; set; }
}
var setting = new Setting { Format = "The car is {colour}" };
var colour = "black";
var output = $"{setting.Format}";
Run Code Online (Sandbox Code Playgroud)
预期产出
"汽车是黑色的".
c# ×5
c#-4.0 ×1
c#-6.0 ×1
compilation ×1
cultureinfo ×1
linq ×1
predicate ×1
settings ×1
wpf ×1
x86-64 ×1