Can I make a C# object's lifetime depend on another object?

I have an object (Delegate) which needs to stay alive (not garbage collected) while another object (TargetObject) is alive. I want Delegate to be garbage collected when TargetObject is collected (or at least available for collection). The difficulty is that I don't want to need to reference Delegate from TargetObject since I want it to work for existing objects unaware of Delegate, and I d

我可以使C#对象的生命周期取决于另一个对象吗?

我有一个对象(委托)需要保持活着(而不是垃圾收集),而另一个对象(TargetObject)是活着的。 当收集TargetObject(或至少可用于收集)时,我希望Delegate被垃圾回收。 困难在于我不想引用TargetObject中的委托,因为我希望它能为不知道Delegate的现有对象工作,并且我不想影响TargetObject的生命周期。 这是可能吗? 谢谢。 编辑:感谢迄今的回应。 我会尽力澄清我的意图。 我试图实现弱事件,但我不是WeakEventM

Is it necessary to explicitly remove event handlers in C#

I have a class that offers up a few events. That class is declared globally but not instanced upon that global declaration--it's instanced on an as-needed basis in the methods that need it. Each time that class is needed in a method, it is instanced and event handlers are registered. Is it necessary to remove the event handlers explicitly before the method goes out of scope? When the me

是否有必要在C#中显式删除事件处理程序

我有一门课提供几个活动。 该类是全局声明的,但没有声明该全局声明 - 它在需要它的方法中根据需要进行实例化。 每次在方法中需要该类时,它都会实例化并注册事件处理程序。 在方法超出范围之前,是否有必要显式移除事件处理程序? 当方法超出范围时,类的实例也会发生。 离开事件处理程序注册到超出范围的实例是否具有内存占用量的含义? (我想知道事件处理程序是否让GC看到不再被引用的类实例。) 谢谢。 在你的

Is this object

I was answering a question about the possibility of closures (legitimately) extending object-lifetimes when I ran into some extremely curious code-gen on the part of the C# compiler (4.0 if that matters). The shortest repro I can find is the following: Create a lambda that captures a local while calling a static method of the containing type. Assign the generated delegate-reference to an in

是这个对象

我回答了关于在C#编译器(如果重要的时候,4.0)遇到一些非常好奇的代码的情况下,关闭(合法地)延长对象生命期的可能性的问题。 我能找到的最短的repro如下: 在调用包含类型的静态方法时创建一个捕获本地的lambda。 将生成的委托引用分配给包含对象的实例字段。 结果:编译器创建一个闭包对象,该闭包对象引用创建lambda的对象,当它没有理由时 - 委托的“内部”目标是静态方法,并且lambda创建对象的实例成员不需要当

Difference between wiring events with and without "new"

In C#, what is the difference (if any) between these two lines of code? tmrMain.Elapsed += new ElapsedEventHandler(tmrMain_Tick); and tmrMain.Elapsed += tmrMain_Tick; Both appear to work exactly the same. Does C# just assume you mean the former when you type the latter? I did this static void Hook1() { someEvent += new EventHandler( Program_someEvent ); } static void Hook2() { so

有和没有“新”的接线事件之间的区别

在C#中,这两行代码之间有什么区别(如果有的话)? tmrMain.Elapsed += new ElapsedEventHandler(tmrMain_Tick); 和 tmrMain.Elapsed += tmrMain_Tick; 两者看起来都一模一样。 当你输入后者时,C#会假设你是指前者吗? 我做到了 static void Hook1() { someEvent += new EventHandler( Program_someEvent ); } static void Hook2() { someEvent += Program_someEvent; } 然后在代码上运行ildasm。 生成

+= new EventHandler(Method) vs += Method

Possible Duplicate: C#: Difference between ' += anEvent' and ' += new EventHandler(anEvent)' There are two basic ways to subscribe to an event: SomeEvent += new EventHandler<ArgType> (MyHandlerMethod); SomeEvent += MyHandlerMethod; What is the difference, and when should I chose one over the other? Edit: If it is the same, then why does VS default to the long version

+ =新的EventHandler(方法)vs + =方法

可能重复: C#:'+ = anEvent'和'+ = new EventHandler(anEvent)'之间的区别' 订阅活动有两种基本方式: SomeEvent += new EventHandler<ArgType> (MyHandlerMethod); SomeEvent += MyHandlerMethod; 有什么区别,我应该什么时候选择一个呢? 编辑:如果它是相同的,那么为什么VS默认为长版本,混乱的代码? 这对我来说毫无意义。 由于我的原始答案似乎有些争议,因此我决定做一些测试

C# pattern to prevent an event handler hooked twice

This question already has an answer here: How to ensure an event is only subscribed to once 6 answers Explicitly implement the event and check the invocation list. You'll also need to check for null: using System.Linq; // Required for the .Contains call below: ... private EventHandler foo; public event EventHandler Foo { add { if (foo == null || !foo.GetInvocationList(

C#模式来防止事件处理程序挂钩两次

这个问题在这里已经有了答案: 如何确保一次活动只订阅了6次答案 显式实现该事件并检查调用列表。 您还需要检查空值: using System.Linq; // Required for the .Contains call below: ... private EventHandler foo; public event EventHandler Foo { add { if (foo == null || !foo.GetInvocationList().Contains(value)) { foo += value; } } remove {

Understanding events and event handlers in C#

I understand the purpose of events, especially within the context of creating user interfaces. I think this is the prototype for creating an event: public void EventName(object sender, EventArgs e); What do event handlers do, why are they needed, and how do I to create one? To understand event handlers, you need to understand delegates. In C#, you can think of a delegate as a pointer (or a

了解C#中的事件和事件处理程序

我理解事件的目的,特别是在创建用户界面的情况下。 我认为这是创建活动的原型: public void EventName(object sender, EventArgs e); 事件处理程序做什么,它们为什么需要,以及如何创建一个? 要理解事件处理程序,您需要了解代表。 在C#中,您可以将委托视为指向方法的指针(或引用)。 这很有用,因为指针可以作为值传递。 代表的中心概念是其签名或形状。 这是(1)返回类型和(2)输入参数。 例如,如果我们

Escaped curly brace in string.format gets lost. Possible C# bug?

I have this MSVC 2012 MCVE: using System; namespace ConsoleApplication1 { internal class Program { private static void Main(string [] args) { string result = string.Format(@"{{{0:G};{1:G}}}", foo(), bar()); Console.WriteLine(result); } private static string foo() { return "foo"; } private static string bar() { return "bar"

string.format中的转义大括号会丢失。 可能的C#错误?

我有这个MSVC 2012 MCVE: using System; namespace ConsoleApplication1 { internal class Program { private static void Main(string [] args) { string result = string.Format(@"{{{0:G};{1:G}}}", foo(), bar()); Console.WriteLine(result); } private static string foo() { return "foo"; } private static string bar() { return "bar"; }

Escape curly brackets around a template entry when using String.Format

Possible Duplicate: String.Format exception when format string contains “{” Does following possible using C# String.Format? Required output "products/item/{itemId}" I've tried escaping braces but this does not work: const string itemIdPattern = "itemId"; string result = String.Format("products/item/{{0}}", itemIdPattern); Preferably something more nice than string result

使用String.Format时,在模板条目周围使用大括号括起来

可能重复: 当格式字符串包含“{”字符串时,String.Format异常 下面可能使用C# String.Format吗? 必需的输出"products/item/{itemId}" 我试过逃避大括号,但这不起作用: const string itemIdPattern = "itemId"; string result = String.Format("products/item/{{0}}", itemIdPattern); 最好比某种更好的东西 string result = String.Format("products/item/{0}{1}{2}", "{

escape curly braces { in a string to support String.Format

I have strings that I read from the database, these strings are fed into String.Format method, if a string has '{' '}' braces but these braces are not escaped correctly for String.Format (ie add another '{' to escape them) the String.Format will throw an exception. The string have any combination of these braces, so in essence the method needs to go through the string an

转义大括号{在一个字符串中支持String.Format

我有从数据库中读取的字符串,这些字符串被送入String.Format方法,如果一个字符串有'{''}'大括号,但这些大括号没有正确地转义为String.Format(即添加另一个'{'转义它们)String.Format将抛出一个异常。 该字符串具有这些大括号的任意组合,因此本质上该方法需要检查字符串并确定'{'是否有关闭字符串,并且如果它们一起形成String.Format的有效占位符(即{5}) ,那些不需要被正确转义的人