[stringtemplate-interest] StringTemplate in C# - attribute lookuprules

Natan Vivo nvivo.misc at gmail.com
Tue Jan 8 13:51:14 PST 2008


Hi. Sorry for the delay...

I took a look at the source code (3.0.1 from the download page):

On Antlr.StringTemplate.Language.ASTExpr, line 574:

foreach (string prefix in new string[] { *"get_", "Get", "Is", "get", 
"is"* })
{
    paramBag.lookupName = prefix + methodSuffix;
    totalReflectionLookups++;
    if (found = *GetMethodValueByName*(paramBag, ref val))
        break;
}

Now, on line 1100, the method *GetMethodValueByName* uses 
*BindingFlags.IgnoreCase*, which makes the capitalized variants useless.

Another thing is that this method also uses *BindingFlags.NonPublic*. ST 
should never look for non public members.

Now, about the order changes, I would say that if there is a 
possibility, those options should simply be excluded from the list or 
added some compatibility option to enable this lookup rule if needed.
C# programmers simply won't code this way, unless they are coming from 
Java, but then, the Java version should look for methods like "Name()" 
because a lazy C# programmer migrating to java might want to code like 
this instead of learning the standards of the language. Makes no sense. =)

C# and VB compilers will create private methods get_XXX and set_XXX for 
properties, but if that is the case, the value will be loaded on the 
property lookup.

Changing the order of the lookup might improve performance a little if 
removal is not an option. But they will never be called if the class 
have an indexer.

Micheal J escreveu:
> Hi,
>  
> Interesting comments. Perhaps the ordering of the various checks can 
> be adjusted so that the less likely (for C#) getXXX, isXXX and GetXXX 
> variants come last?
>  
> Micheal
>
>     ------------------------------------------------------------------------
>     *From:* stringtemplate-interest-bounces at antlr.org
>     [mailto:stringtemplate-interest-bounces at antlr.org] *On Behalf Of
>     *Natan Vivo
>     *Sent:* 27 December 2007 16:16
>     *To:* stringtemplate-interest at antlr.org
>     *Subject:* [stringtemplate-interest] StringTemplate in C# -
>     attribute lookuprules
>
>     Why are there so many lookup rules for C# in contrast to Java or
>     Python versions?
>
>     In this page
>     http://www.antlr.org/wiki/display/ST/Expressions#Expressions-Propertyreferences
>     <http://www.antlr.org/wiki/display/ST/Expressions#Expressions-Propertyreferences>,
>     it tells me C# version would try to get a value from 7 different
>     places before trying an indexer.
>
>     I ask this because an application I wrote makes extensive use of
>     indexers to add custom attributes to an object, and although the
>     performance is good enough, I just wonder if this lookup rules
>     aren't too bloated.
>
>     For example, why to have rules for get_name, getname and Getname?
>     The probability of someone writing like this in C# is the same of
>     writing a method called Name() in java. The property X in c# is
>     the direct equivalent of getX() in java.
>
>     This also makes my indexer called "type" be dropped in favor of
>     the method GetType on the System.Object, making me declare a
>     property called "type" calling the indexer.
>
>     I would suggest that this rule to be 1. Property, 2. IsProperty,
>     3. indexer, but I guess that would cause a lot of compatibility
>     problems by now.
>
>
>     Also I just found out that ST will look for private members too on
>     attribute lookup. Shoudn't it look for public instance members only?
>
>     Thanks,
>     Natan Vivo
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
>   

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20080108/12880d10/attachment.html 


More information about the stringtemplate-interest mailing list