2 using System
.Collections
;
3 using System
.Collections
.Generic
;
5 using System
.Reflection
;
6 using Castle
.MonoRail
.Framework
;
8 namespace Castle
.MonoRail
.Views
.Brail
10 public class DslProvider
: IQuackFu
12 private readonly BrailBase view
= null;
13 public DslProvider(BrailBase view
)
16 foreach (PropertyInfo prop
in typeof(BrailBase
).GetProperties(BindingFlags
.Public
| BindingFlags
.Instance
))
18 if (prop
.Name
== "Dsl" || prop
.Name
== "ViewEngine" || prop
.Name
== "Properties" || prop
.CanRead
== false)
23 viewProperties
.Add(prop
.Name
, prop
.GetGetMethod());
27 private IDslLanguageExtension currentExtension
= null;
28 private readonly IDictionary
<string, MethodInfo
> extensionMethods
= new Dictionary
<string, MethodInfo
>();
29 private readonly IDictionary
<string, MethodInfo
> viewProperties
= new Dictionary
<string, MethodInfo
>();
31 public void Register(IDslLanguageExtension dslExtension
)
33 if (currentExtension
== null)
35 foreach (MethodInfo method
in dslExtension
.GetType().GetMethods(BindingFlags
.Public
| BindingFlags
.Instance
))
37 if(method
.DeclaringType
==typeof(object))
39 string name
= CreateMethodKey(method
.Name
, method
.GetParameters());
40 extensionMethods
.Add(name
, method
);
42 currentExtension
= dslExtension
;
46 private string CreateMethodKey(string methodName
, object[] parameters
)
48 return string.Format("{0}_{1}", methodName
, parameters
.GetLength(0));
51 #region IQuackFu Members
53 public object QuackGet(string name
, object[] parameters
)
55 if (view
.IsDefined(name
))
57 return view
.GetParameter(name
);
60 if (viewProperties
.ContainsKey(name
))
62 return viewProperties
[name
].Invoke(view
, null);
68 public object QuackInvoke(string name
, params object[] args
)
70 string methodName
= CreateMethodKey(name
, args
);
71 if (extensionMethods
.ContainsKey(methodName
))
73 MethodInfo method
= extensionMethods
[methodName
];
74 method
.Invoke(currentExtension
, args
);
77 //didn't find it in the hard coded methods, so we want to raise the generic ones.
81 currentExtension
.Tag(name
);
84 currentExtension
.Tag(name
, (ICallable
)args
[0]);
87 currentExtension
.Tag(name
, (IDictionary
)args
[0],(ICallable
)args
[1]);
93 public object QuackSet(string name
, object[] parameters
, object value)
95 throw new Exception("The method or operation is not implemented.");