Questions tagged «c#»

C#(发音为“ See Sharp”)是由Microsoft开发的一种高级,静态类型的多范例编程语言。C#代码通常针对Microsoft的.NET系列工具和运行时,其中包括.NET Framework,.NET Core和Xamarin。使用此标记可解决有关用C#或C#正式规范编写的代码的问题。


8
System.IO。包装
我的项目设置为.NET Framework 4.0。当我添加System.IO.Packaging,它说它不存在。当我尝试将其添加为对项目的引用时,它也不会显示。 如何添加System.IO.Packaging到我的C#项目中?

20
目录不存在。参数名称:directoryVirtualPath
想要改善这篇文章吗?提供此问题的详细答案,包括引文和答案正确的解释。答案不够详细的答案可能会被编辑或删除。 我刚刚将项目发布到Arvixe上的主机上,并收到此错误(在本地工作正常): Server Error in '/' Application. Directory does not exist. Parameter name: directoryVirtualPath Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentException: Directory does …

10
保持ASP.NET会话开放/有效
只要用户打开了浏览器窗口,哪种方法最容易且最不引起干扰的方式就可以使ASP.NET会话保持活动状态?是定时的AJAX通话吗?我要防止出现以下情况:有时用户长时间保持窗口打开状态,然后输入内容,并且由于服务器端会话已过期,因此提交后再无任何作用。我不想在服务器上将超时值增加超过10分钟,因为我希望关闭的会话(通过关闭浏览器窗口)来快速超时。 建议,代码示例?
115 c#  asp.net  session 

4
首先使用EF代码映射复合键
SQL Server表: SomeId PK varchar(50) not null OtherId PK int not null 我应该如何首先在EF 6代码中映射它? public class MyTable { [Key] public string SomeId { get; set; } [Key] public int OtherId { get; set; } } 我看过一些示例,您必须为每个列设置顺序,这是必需的吗? 在某处有官方文件吗?

2
实体框架-手动添加导航属性
我从数据库中生成了一个实体框架模型(4.0)。我没有设计数据库,也没有对架构的任何控制,但是有一些表没有定义外键约束,但是定义了隐式关系。 例如: 我有一个名为People的表,该表具有以下列:GenderID RaceID 有“性别”和“种族”表,但“人”表中没有外键。 当我导入模型时,它没有为这些关系添加导航属性。我尝试手动添加它,但是“从角色”和“到角色”被禁用。我不确定如何自己添加关系。我该怎么做呢?


6
无效的URI:无法确定URI的格式
我不断收到此错误。 无效的URI:无法确定URI的格式。 我的代码是: Uri uri = new Uri(slct.Text); if (DeleteFileOnServer(uri)) { nn.BalloonTipText = slct.Text + " has been deleted."; nn.ShowBalloonTip(30); } 更新: slct.Text中的内容为ftp.jt-software.net/style.css。 是什么赋予了?这怎么不是有效的URI格式?它是纯文本。
115 c#  .net  winforms  uri 


9
正确使用.NET MemoryCache的锁定模式
我认为这段代码存在并发问题: const string CacheKey = "CacheKey"; static string GetCachedData() { string expensiveString =null; if (MemoryCache.Default.Contains(CacheKey)) { expensiveString = MemoryCache.Default[CacheKey] as string; } else { CacheItemPolicy cip = new CacheItemPolicy() { AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(20)) }; expensiveString = SomeHeavyAndExpensiveCalculation(); MemoryCache.Default.Set(CacheKey, expensiveString, cip); } return expensiveString; } 并发问题的原因是多个线程可以获取空键,然后尝试将数据插入缓存。 进行此代码并发证明的最短,最干净的方法是什么?我喜欢在与缓存相关的代码中遵循良好的模式。链接到在线文章会很有帮助。 更新: 我根据@Scott Chamberlain的答案提出了此代码。有人能找到与此相关的任何性能或并发问题吗?如果可行,它将节省很多代码和错误。 …


8
我可以使用反射更改C#中的私有只读字段吗?
我想知道,由于反射可以完成很多事情,构造函数完成执行后是否可以更改私有只读字段? (注意:只是好奇心) public class Foo { private readonly int bar; public Foo(int num) { bar = num; } public int GetBar() { return bar; } } Foo foo = new Foo(123); Console.WriteLine(foo.GetBar()); // display 123 // reflection code here... Console.WriteLine(foo.GetBar()); // display 456
115 c#  reflection  field  readonly 

9
C#捕获堆栈溢出异常
我有一个递归调用一个引发堆栈溢出异常的方法。第一次调用被try catch块包围,但未捕获异常。 堆栈溢出异常是否以特殊方式表现?我可以正确捕获/处理异常吗? 不确定是否相关,但还有其他信息: 在主线程中没有抛出异常 代码引发异常的对象由Assembly.LoadFrom(...)。CreateInstance(...)手动加载

10
为什么词典没有AddRange?
标题已经足够基本了,我为什么不能: Dictionary<string, string> dic = new Dictionary<string, string>(); dic.AddRange(MethodThatReturnAnotherDic());
115 c#  .net  dictionary 

10
用C#修剪字符串中的换行符最简单的方法是什么?
我想确保_content不以换行符结尾: _content = sb.ToString().Trim(new char[] { Environment.NewLine }); 但是上面的代码不起作用,因为Trim似乎没有字符串集合(只有字符)的重载参数。 从字符串末尾删除Enivronment.Newline的最简单的单行代码是什么?
115 c#  string 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.