Tag: .net

使用Reactive Extensions重新排序事件

我正在尝试重新排序在不同线程上无序的事件。 是否可以创建匹配这些大理石图的反应式扩展查询: s1 1 2 3 4 s2 1 3 2 4 result 1 2 3 4 和… s1 1 2 3 4 s2 4 3 2 1 result 1234 即:仅以版本号顺序发布结果。 我最接近的是每次s1时刻都使用Join打开一个窗口,只有当s2到达时才使用相同的数字关闭它。 像这样: var publishedEvents = events.Publish().RefCount(); publishedEvents.Join( publishedEvents.Scan(0, (i, o) => i + 1), expectedVersion => publishedEvents.Any(@event => @event.Version == expectedVersion), _ => Observable.Never(), […]

修复通用UserControl的嵌入资源

在重构期间,我向MyControl添加了一个generics类型参数, MyControl是一个派生自UserControl的类。 所以我的class级现在是MyControl 。 现在我在运行时收到一条错误,指出无法找到嵌入式资源文件MyControl`1.resources 。 使用.NET Reflector快速查看显示资源文件实际上名为MyControl.resources ,没有`1 。 在MyControl.InitializeComponent方法的开头,有一行可能是导致问题的那一行: System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager( typeof(MyControl)); 如何强制ComponentResourceManager使用嵌入式资源文件MyControl.resources ? 其他解决此问题的方法也是受欢迎的。

C#垃圾收集器如何找到唯一引用为内部指针的对象?

在C#中,据我所知, ref和out params只传递相关值的原始地址。 该地址可以是指向数组中的元素或对象内的字段的内部指针。 如果发生垃圾收集,则对某个对象的唯一引用可能是通过其中一个内部指针,如: using System; public class Foo { public int field; public static void Increment(ref int x) { System.GC.Collect(); x = x + 1; Console.WriteLine(x); } public static void Main() { Increment(ref new Foo().field); } } 在这种情况下,GC需要找到对象的开头并将整个对象视为可达。 它是如何做到的? 是否必须扫描整个堆以查找包含该指针的对象? 这似乎很慢。

LDAPS连接出现未知错误(0x80005000)

在烦人的Active Directory位上,我最近几个小时都被困住了。 我想要完成的是通过SSL上的LDAP连接到Active Directory。 身份validation类型是匿名的。 我正在使用.NET Framework 4.0,C#和Visual Studio 2010。 以下代码应根据各种在线资源工作。 但它不断提出令人惊讶的不言自明:“未知错误(0x80005000)”。 DirectoryEntry entry = new DirectoryEntry(); entry.Path = “LDAPS://some.ldap.server:636”; entry.AuthenticationType = AuthenticationTypes.SecureSocketsLayer; DirectorySearcher searcher = new DirectorySearcher(); searcher.searchRoot = entry; searcher.Filter = “(&(objectCategory=person)(objectClass=user))”; SearchResultCollection results = searcher.FindAll(); 我已经将我想要执行的实际查询简化为您在代码中找到的查询。 但即使使用这种通用查询(它应该返回每个AD的工作?),它也会返回错误。

通用约束如何防止使用隐式实现的接口装箱值类型?

我的问题与此问题有些相关: 显式实现的接口和通用约束 。 然而,我的问题是编译器如何启用通用约束来消除对显式实现接口的值类型进行装箱的需要。 我想我的问题归结为两个部分: 在访问显式实现的接口成员时需要将值类型装箱的幕后CLR实现发生了什么,以及 删除此要求的通用约束会发生什么? 一些示例代码: internal struct TestStruct : IEquatable { bool IEquatable.Equals(TestStruct other) { return true; } } internal class TesterClass { // Methods public static bool AreEqual(T arg1, T arg2) where T: IEquatable { return arg1.Equals(arg2); } public static void Run() { TestStruct t1 = new TestStruct(); TestStruct t2 = […]

使用C#压缩单个文件

我正在使用.NET 4.5,如果我尝试使用“CreateFromDirectory”压缩整个目录,ZipFile类的效果很好。 但是,我只想在目录中压缩一个文件。 我试着指向一个特定的文件(文件夹\ data.txt),但这不起作用。 我考虑过ZipArchive类,因为它有一个“CreateEntryFromFile”方法,但似乎这只允许你创建一个现有文件的条目。 有没有办法简单地压缩一个文件而不创建一个空的zipfile(有它的问题),然后使用ZipArchiveExtension的“CreateEntryFromFile”方法? **这也是假设我正在开发一个目前无法使用第三方附加组件的公司计划。 示例来自: http : //msdn.microsoft.com/en-us/library/ms404280%28v=vs.110%29.aspx string startPath = @”c:\example\start”; string zipPath = @”c:\example\result.zip”; string extractPath = @”c:\example\extract”; ZipFile.CreateFromDirectory(startPath, zipPath); ZipFile.ExtractToDirectory(zipPath, extractPath); 但是如果startPath是@”c:\example\start\myFile.txt;” ,它会抛出一个目录无效的错误。

“等待Task.Yield()”及其替代品

如果我需要推迟代码执行,直到UI线程消息循环的未来迭代之后,我可以这样做: await Task.Factory.StartNew( () => { MessageBox.Show(“Hello!”); }, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()); 这类似于await Task.Yield(); MessageBox.Show(“Hello!”); await Task.Yield(); MessageBox.Show(“Hello!”); 除此之外,如果我愿意,我可以选择取消任务。 在使用默认同步上下文的情况下,我可以类似地使用await Task.Run继续池线程。 事实上,我比Task.Run更喜欢Task.Factory.StartNew和Task.Run ,因为它们都明确定义了延续代码的范围。 那么,在什么情况下await Task.Yield()实际上是有用的?

C#是否具有“ThreadLocal”模拟(对于数据成员)到“ThreadStatic”属性?

我发现“ThreadStatic”属性最近非常有用,但现在我想要一个“ThreadLocal”类型属性,它允许我在每个线程的基础上拥有非静态数据成员。 现在我知道这会产生一些非平凡的影响,但是: C#/ .net中是否已经内置了这样的东西? 或者因为到目前为止它的答案是否定的(对于.net <4.0),是否有一个常用的实现? 我可以想出一个合理的方法来实现它,但只会使用已经存在的东西,如果可用的话。 如果它还不存在,那么它将实现我正在寻找的东西: class Foo { [ThreadStatic] static Dictionary threadLocalValues = new Dictionary(); int defaultValue = 0; int ThreadLocalMember { get { int value = defaultValue; if( ! threadLocalValues.TryGetValue(this, out value) ) { threadLocalValues[this] = value; } return value; } set { threadLocalValues[this] = value; } } } 请原谅任何C#无知。 我是一名C […]

在visual studio中重建app.config?

我的app.config给了我几条与“找不到usersettings schema”有关的消息,所以我删除它是为了从头开始构建它,但我没有成功。 如何根据我的设置文件构建一个新的? 每当我创建一个新的并构建解决方案时,它都不会构建应用程序配置文件。 我不知道还有什么可以尝试的。 这是我旧文件的麻烦部分: DejaVu Sans, 20.25pt, style=Italic False

哪个更快:Automapper,Valuinjector还是手动映射? 每个人的速度到什么程度?

假设我的DAL(ORM等)中有这个对象 public class Student { public string Name {get;set;} public string Address {get;set;} public string Phone {get;set;} public Parent Parent {get;set;} } public class Parent { public string Name {get;set;} public string Address {get;set;} public string Phone {get;set;} } 我有一个看起来像这样的ViewModel public class StudentDetailVM { public string Name {get;set;} public string Address {get;set;} public string Phone […]