This is C # 4.0.
I have a class that stores WeakReference on some Action , like this:
public class LoremIpsum { private Dictionary<Type, List<WeakReference>> references = new Dictionary<Type, List<WeakReference>>(); public void KeepReference<T>(Action<T> action) { if (this.references.ContainsKey(typeof(T))) { this.references[typeof(T)].Add(new WeakReference(action)); } else { this.references.Add(typeof(T), new List<WeakReference> { new WeakReference(action) }); } } }
This class has another method that allows you to execute an Action passed to it later, but it has little value in this matter.
and I use this class as follows:
public class Foobar { public Bar Bar { get; set; } public void Foo(LoremIpsum ipsum) { ipsum.KeepReference<Bar>((b) => { this.Bar = b; }); ipsum.KeepReference<Bar>(this.Whatever); } public void Whatever(Bar bar) {
Bar is the third class in my application.
My question is:
In the KeepReference method, how can I find out if the Action parameter refers to an anonymous method parameter ( this.Bar = b; ) or a specific method ( this.Whatever )?
I checked the properties of Action . I could not find any property on the Action (e.g. IsAbstract ) like IsAnonymous . The main type MethodInfo makes sense, because after compilation I see in ildasm an anonymous method "became" a regular Foobar method. In ildasm, I also see that the anonymous method is not a full pink square, but a white square surrounded by a pink one, and in its definition there is a call to some CompilerServices classes, but I do not know how to use this in C #. I am sure that you can learn about the real nature of Action . What am I missing?
source share