[nem-en] Finding an interface method implementation.
Kamil Skalski
nazgul at nemerle.org
Mon Sep 27 14:03:22 CEST 2004
Dnia poniedziałek, 27 września 2004 13:13, Daniel James napisał:
> I'm trying to write a macro to automatically dispose of a field. For a
> class like this:
Nice idea, let take a look how to do it.
> class Foo : System.IDisposable
> {
> [Utility.AutoDispose]
> bar : Bar;
>
> private MyDispose() : void
> implements System.IDisposable.Dispose
> {
> }
> }
The macro should be in WithTypedMembers phase and perform something like:
macro AutoDispose (t : TypeBuilder, f : FieldBuilder) {
/// probalby cache it somewhere for multile executions of this macro
def system_idisposable_dispose =
typeof (System.IDisposable).GetMethods (....)
...
/// do the boring stuff to obtain MethodInfo of IDisposable.Dispose
def methods = t.GetMethods (... BindingFlags.DeclaredOnly... Instance .
etc.);
def candidates = List.Filter (mehods, fun (m : IMethod) {
match (m.GetFunKind ()) {
| FK_bound_method (implemented) =>
List.Filter (implemented, fun (im) {
im.GetMethodInfo ().Equals (system_idisposable_dispose)
}
| _ => false
};
/// probably assume, there is only one candidate or scream if not
// modify its body
}
I'm not sure if this is the best and most efficient way to do this, but
current implementaion of compiler seems to do such things from time to time.
Maybe Michal can come out with some nicer way to do this.
Probably we should add a special API make it easier. We will take your
question as a vote, that this particular API would be nice to have.
Any suggestions are welcomed - currently API is created when needed and when
somebody has some nice idea for useful methods.
> Utility.AutoDispose needs to modify MyDispose's body, but I've no idea
> how to find it - at the moment my code just looks for a method called
> Dispose. Is this possible?
Well, this is quite strightforward approach and seems to work well. But of
course looking at implemented interface methods is more complete.
--
Kamil Skalski
http://nemerle.org developer
More information about the devel-en
mailing list