[stringtemplate-interest] C# indexer - no attribute in template context
Dustin A. Lambert
dustin at biztechetc.com
Fri Jul 23 19:54:55 PDT 2010
I think you have to implement IDictionary with the C# version to get it to
use the indexer...
For my projects, I have a dictionary base that just satisfies the
IDictionary requirements... So you just inherit from the base and then
override the indexer.
IIRC, ST calls the Contains method first to verify that the value exists,
then uses the indexer.
public abstract class DictionaryBase : IDictionary {
#region IDictionary
public virtual void Add(object key, object value) {
throw new NotImplementedException(); }
public virtual void Clear() { throw new
NotImplementedException(); }
public virtual bool Contains(object key) {
return true; }
public virtual IDictionaryEnumerator GetEnumerator() {
throw new NotImplementedException(); }
public virtual bool IsFixedSize { get {
throw new NotImplementedException(); } }
public virtual bool IsReadOnly { get {
return true; } }
public virtual ICollection Keys { get {
throw new NotImplementedException(); } }
public virtual void Remove(object key) {
throw new NotImplementedException(); }
public virtual ICollection Values { get {
return new object[0]; } }
public abstract object this[object key] {
get; set; }
public virtual void CopyTo(Array array, int index) {
throw new NotImplementedException(); }
public virtual int Count { get { return 1; } }
public virtual bool IsSynchronized { get {
throw new NotImplementedException(); } }
public virtual object SyncRoot { get { throw
new NotImplementedException(); } }
IEnumerator IEnumerable.GetEnumerator() {
throw new NotImplementedException(); }
#endregion }
I may be wrong about this... using ST 3.2 C# port here.
Hope this helps,
Dustin
On Fri, Jul 23, 2010 at 12:25 PM, Tomasz Mloduchowski <tomasz at ourscale.eu>wrote:
> Hi!
>
> I'm new to StringTemplate, and I've been trying to use it with an CLR
> indexer.
>
> I've boiled down my troubles to this simple program:
>
> using System;
> using Antlr3.ST;
>
> public class Lang {
> public string this [ string key ] {
> set {}
> get {return "Foo"; }
> }
> }
>
> public class HelloWorld {
> static public void Main (){
> Console.WriteLine("Hi");
>
> Lang l = new Lang();
> Console.WriteLine(l["XX"]);
>
> StringTemplate hello = new StringTemplate("Hello, $name$: $lang.XX$");
> hello.SetAttribute("name", "World");
> hello.SetAttribute("lang", l);
> Console.WriteLine(hello.ToString());
> }
> }
>
> If I'm reading the documentation located here correctly,
> http://www.antlr.org/wiki/display/ST/Expressions
>
> this should just output:
>
> Hi
> XX
> Hello World: XX
>
>
> Unfortunately, I'm getting:
> Hi
> Foo
> Class Lang has no such attribute: XX in template context [anonymous]
> Hello, World:
>
> Do you have a suggestion? Google reveals nothing relevant.
>
> Cheers,
> Tomasz
>
>
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20100723/0bd4cd78/attachment.html
More information about the stringtemplate-interest
mailing list