Sunday, July 18, 2010

ComponentSoft Template Engine Calling Restriction

UltimateTemplateEngine allows you to restrict the call to specific namespaces, methods and properties, you can allow and restrict the call to namespaces, methods and properties by using wildcard characters (*,?) or extract names as well.

The following example shows how to use RestrictedMethodList, AllowedMethodList, RestrictedPropertyList and AllowedPropertyList of TemplateEngine class.
C# Copy Code
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ComponentSoft;

namespace MethodAndPropertyRestriction
{
public static class MyTestClass
{
public static string TestString
{
get { return "Static String"; }
}
public static string GetStaticString(string s)
{
return string.Format("GetStaticString returns {0}", s);
}
}
public static class ClassRestricted
{
public static string TestString
{
get { return "Test String in a restricted class"; }
}
}
public static class MethodAndProperty
{
public static string TestStringRestricted
{
get { return "Restricted String"; }
}
public static string GetStaticStringRestricted(string s)
{
return string.Format("GetStaticStringRestricted returns {0}", s);
}
public static string StaticStringAllowed
{
get
{
return "StaticStringAllowed";
}
}
}
static class Program
{
///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
try
{
UltimateTemplateEngine ctl = new UltimateTemplateEngine();
ctl.LoadFromString("$using(\"MethodAndPropertyRestriction,MethodAndPropertyRestriction\")$$MyTestClass.TestString$\r\n$MyTestClass.GetStaticString(\"Test\")$\r\n$ClassRestricted.TestString$\r\n$MethodAndProperty.TestStringRestricted$\r\n$MethodAndProperty.GetStaticStringRestricted("static string")$\r\n$MethodAndProperty.StaticStringAllowed$");
ctl.RestrictedMethodList.Add("*.ClassRestricted.*");
ctl.RestrictedPropertyList.Add("*.ClassRestricted.*");
ctl.RestrictedMethodList.Add("*.MethodAndProperty.GetStaticString*");
ctl.AllowedMethodList.Add("*.MethodAndProperty.GetStaticStringAllowed");
ctl.RestrictedPropertyList.Add("*.TestStringRestricted");
string s = ctl.Run();
MessageBox.Show(s);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
}
}

No comments:

Post a Comment