所有Visual Studios(2012年)也不格式化以下内容:
_messageProcessor = new Dictionary<ServerDataTypes, MessageProcessor>()
{
{ServerDataTypes.FrameData, ProcessFrameData } ,
{ ServerDataTypes.ServerStatusResult,ProcessServerStatusResult },
{ ServerDataTypes.PlayerMessage, ProcessPlayerMessage},
....
};
Run Code Online (Sandbox Code Playgroud)
如何使我的Visual Studio 2010(或2012)自动格式化?我需要以下结果:
_messageProcessor = new Dictionary<ServerDataTypes, MessageProcessor>()
{
{ ServerDataTypes.FrameData, ProcessFrameData },
{ ServerDataTypes.ServerStatusResult, ProcessServerStatusResult },
{ ServerDataTypes.PlayerMessage, ProcessPlayerMessage },
...
};
Run Code Online (Sandbox Code Playgroud)
就像在新创建的对象的自动属性中一样.格式正在为此工作.但不是为了这个.那么,如何解决呢?
我正在编写表达式扩展方法,它必须反转bool-typed lambda表达式.
这是我在做的事情:
public static Expression<Func<T, bool>> Inverse<T>(this Expression<Func<T, bool>> e)
{
return Expression.Lambda<Func<T, bool>>(Expression.Not(e));
}
Run Code Online (Sandbox Code Playgroud)
但这引起了例外,那就是unary operator is NOT not defined for the type Func<int,bool>.我也试过这个:
public static Expression<Func<T, bool>> Inverse<T>(this Expression<Func<T, bool>> e)
{
return Expression.Lambda<Func<T, bool>>(Expression.Not(e.Body));
}
Run Code Online (Sandbox Code Playgroud)
但得到这个:Incorrent number of parameters supplied for lambda declaration.
这个StackOverflow 答案完全描述了 aHashSet是无序的,它的项目枚举顺序是未定义的,不应依赖。
然而,
这就引出了另一个问题:我应该还是不应该依赖两个或多个后续枚举之间的枚举顺序?鉴于没有插入或删除。
例如,假设我向 HashSet 添加了一些项目:
HashSet<int> set = new HashSet<int>();
set.Add(1);
set.Add(2);
set.Add(3);
set.Add(4);
set.Add(5);
Run Code Online (Sandbox Code Playgroud)
现在,当我通过 枚举这个集合时foreach,假设我收到了这个序列:
// Result: 1, 3, 4, 5, 2.
Run Code Online (Sandbox Code Playgroud)
问题是:如果我没有修改,如果我再次枚举设置的时间和时间,订单会保留吗?会永远一样吗?
在这段代码中......
public static TransactionScope CreateTransactionScope(bool createNew = false)
{
return new TransactionScope(
createNew ? TransactionScopeOption.RequiresNew : TransactionScopeOption.Required,
new TransactionOptions() { IsolationLevel = IsolationLevel.ReadCommitted });
}
Run Code Online (Sandbox Code Playgroud)
实际上,在这一个......
using (TransactionScope rootScope = CreateTransactionScope())
{
using (TransactionScope nestedOne = CreateTransactionScope())
{ nestedOne.Complete(); }
using (TransactionScope nestedTwo = CreateTransactionScope(true))
{ nestedTwo.Complete(); }
// No committing, rollback 'rootScope'.
}
Run Code Online (Sandbox Code Playgroud)
什么交易将会陆续与根沿一个回-不会是唯一的nestedOne或两者nestedOne并nestedTwo?
看,我正在开发一个MVC4项目的网站.该站点通常以非常简单的方式查询其数据库中的各种设置:
private static T GetSetting<T>(string parameterName)
{
return (T)Convert.ChangeType(/* bla bla SQL query code */, typeof(T));
}
Run Code Online (Sandbox Code Playgroud)
逻辑上的问题是 - 什么,你每次想要获得任何设置时都在查询数据库吗?如果你需要在一些for周期或东西中怎么办?
所以我想出了缓存解决方案,我需要知道我能做到最好.你怎么看?
internal static void ClearCache()
{
foreach (IDictionary cache in _caches)
cache.Clear();
}
private static readonly HashSet<IDictionary> _caches = new HashSet<IDictionary>();
private static class TypedCache<T>
{
private static readonly Dictionary<string, T> _cache = new Dictionary<string, T>();
internal static Dictionary<string, T> Cache { get { return _cache; } }
}
private static T GetSetting<T>(string parameterName)
{
T value; …Run Code Online (Sandbox Code Playgroud) 我说有3个项目:一个是MVC2应用程序,第二个是Windows服务和服务使用的dll库.我可以安装和卸载服务到系统,但我需要MVC在服务启动时"打开",在关闭时"关闭".
像IIS这样的任何其他东西更合适,但我们说它们不可用.
对不起我的英语不好.换句话说,我需要在C#Windows服务中托管MVC Web应用程序.
让我说我有这个:
class A { }
class B : A { }
class C : B { }
class Foo
{
public void Bar(A target) { /* Some code. */ }
}
class AdvancedFoo : Foo
{
public void Bar(B target)
{
base.Bar(target);
// Some code.
}
}
sealed class SuperiorFoo : AdvancedFoo
{
public void Bar(C target)
{
base.Bar(target);
// Some code.
}
}
Run Code Online (Sandbox Code Playgroud)
如果我跑new SuperiorFoo().Bar(new C()),为什么会调用哪个重载?我猜它会被级联调用,但我无法弄清楚为什么以及这种行为是否得到保证.
更新
所以,base.与这两部作品Foo,并AdvancedFoo为SuperiorFoo,所以哪一个将被调用,为什么呢?
我试图枚举运行时输入以在c#中打印枚举变量的值.例如,
class Program
{
enum Alphabets { a = 1, b, c, d, e, f, g, h }
public static void Main(String[] args)
{
string s = Console.ReadLine();
foreach(char c in s)
{
foreach(int i in Enum.GetValues(typeof(Alphabets)))
Console.WriteLine(s[i]);
}
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
我将用户输入存储在String中.我需要显示用户提供的字符串的整数值.上面的代码显示了如下错误:
我怎么能纠正这个?或者请给我一个有效的代码..
我有列表视图和一些列"ID","产品名称"和"供应商".我可以添加,编辑或删除这些项目,并在"弄乱"项目列表视图后刷新这样的东西.
listView.Items.Clear();
foreach (var item in result)
{
ListViewItem lv = new ListViewItem(item.ID.ToString());
lv.SubItems.Add(item.Name.ToString());
lv.SubItems.Add(item.Vendor.ToString());
listView.Items.Add(lv);
}
Run Code Online (Sandbox Code Playgroud)
所以这个方法工作正常,除非每次刷新列表焦点的项目丢失,这是合乎逻辑的,因为我从列表中删除了所有项目并再次填充它.所以我的问题是我如何能够专注于编辑和新添加的项目,主要是当我编辑一些项目时,我希望listview不要滚动到顶部,而是留在编辑项目所在的位置.我尝试使用方法FindItemWithText,而不是将listview的焦点项设置为找到的项,但它不起作用.这种问题有解决办法吗?
我有这个系列
ObservableCollection<string> Urls { get; set; }
Run Code Online (Sandbox Code Playgroud)
在我的数据上下文类中.我在列表框中绑定了它:
<ListBox ItemsSource="{Binding Urls}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding .}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
数据显示在列表框中,两个相应的 - 未显示 - 这里的按钮带有命令Add和Delete也可以工作,但是,更改TextBox不会影响集合的内容.
我试过Mode=TwoWay绑定,但我认为它已经打开了.我已经尝试了一些其他选项,如Validate = OnPropertyChange,但是,仍然没有任何更新.
如何使实际TextBox模板中的内部ListBox实际更新Urlsdatacontext类的属性?
我有这门课:
public abstract class MyController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
string viewPath = filterContext/*... .ViewPath*/;
viewPath = "Some new View Path";
}
}
Run Code Online (Sandbox Code Playgroud)
我想用另一个检索并替换正在执行的视图的路径.我试图在Web调用上调试 - 查看过滤器上下文,但是我没有设法找到即将呈现的视图.
我怎么做?
我知道这是可能的 -在创建窗口时通过SetWindowLongAPI 或 WPFWindow类的托管属性以某种方式......但我不知道如何做到这一点。我根本找不到有关如何设置窗口样式的信息,因此它无法接收有关鼠标单击它的任何系统消息,并且任何单击都会通过窗口到达底层窗口。
有谁知道那个样式代码之类的?
我目前使用的Team Foundation Server作为source control,然而,它的数据库位于我的电脑上,我希望它是某处在互联网上,在一个小的免费云可能.
是否有免费source control系统,内置VS2012支持和免费存储库存储?这些是什么?
我有一点谷歌搜索,但有点难以察觉是系统支持互联网托管的存储库和VS2012支持.我需要真实用户的意见.
c# ×12
.net ×2
wpf ×2
asp.net-mvc ×1
c#-4.0 ×1
caching ×1
data-binding ×1
enumeration ×1
enums ×1
expression ×1
focus ×1
foreach ×1
hashset ×1
hittest ×1
inheritance ×1
lambda ×1
listbox ×1
listview ×1
methods ×1
msdn ×1
overloading ×1
repository ×1
rollback ×1
sequence ×1
service ×1
settings ×1
sql-server ×1
templates ×1
tfs ×1
transactions ×1
view ×1
window ×1