Dnia czwartek, 13 stycznia 2005 14:47, Stephen Touset napisaÅ:
> Shawn Vose wrote:
> > Use System.Reflection to read available methods, attributes and
> > properties for a dll. You can use it to do so at runtime. I know there
> > has been a few questions and answers on Relection in the archives
>
> What I'm trying to accomplish is slightly different, and cannot be done
> with the current design of attributes.
Of course it is reasonable that you want to stick in C# and you will have to
change architecture of mcs to accomplish it. Implementation, which you are
describing is exactly what we do in Nemerle. Compiler is looking at
attrubutes and for some of them it runs a macro operating on compiler's
internal structures.
So instead (besides) of attatching some metadata to generated assembly, it
runs external program modifing method's body. Seeing this feature in mcs
would be nice.
Probably a little problem would be easy generation of code - you will have to
use mcs's API to build syntax trees. In Nemerle we have special syntax for
this, so implementation of pre-condition macro Requires is:
/// Example: [Requires (i != 4 && boo (), throw ArgumentException ())] foo
(i : int) : void { ... }
/// Example: [Requires (i != 4)] foo (i : int) : void { ... }
[Nemerle.MacroUsage (Nemerle.MacroPhase.BeforeInheritance,
Nemerle.MacroTargets.Method,
Inherited = true, AllowMultiple = true)]
macro Requires (_ : TypeBuilder, m : ParsedMethod, assertion, otherwise =
null)
{
def check =
if (otherwise != null)
<[ unless ($assertion) $otherwise ]>
else
<[ assert ($assertion, "The ``Requires'' contract of method `" +
$(m.name.GetName ().Id : string) + "' has been
violated.") ]>;
m.Body = <[
$check;
$(m.Body)
]>
}
Here, generated program it is quite small, but in case of invariant it is much
more complex.
In future we are planing to provide a C# fronend to Nemerle compiler, so you
would have Design By Contrace also in pure C#.
>
> As an example, I want to be able to place preconditions and
> postconditions on methods and invariants on classes. These attributes
> need to follow inheritance rules for Design By Contract as well:
> postconditions are logically ORed with any inherited postconditions from
> base classes, while postconditions and invariants are logically ANDed
> with inherited ones from their base classes. This is not possible with
> the current implementation of attributes.
>
> Nor is the ability for an attribute to consitently, automatically, and
> predictable modify the way a method or class works. Attributes cannot
> access the methods they belong to, nor can they generate code
> dynamically. There is also no way to have them transparently invoke code
> upon method execution.
>
> Because of this, something would need to be written to have certain
> attributes generate code in the methods they modify, or in all the
> methods of the classes they modify. Also, these attributes would need to
> be inherited upon their base classes as well, automatically. A script
> could theoretically do it, but that would require a lot of additional
> work, reimplementing a C# parser that already exists with Mono.
>
> If you can find a way to do this with System.Reflection, please let me
> know. It would save me a bundle of time.
> _______________________________________________
> Mono-list maillist - [email protected]
> http://lists.ximian.com/mailman/listinfo/mono-list
_______________________________________________
Mono-list maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-list