WHEN you have read The Posts on My Blog, you have probably seen the new expression like the <% $ resource ...%>, <% $ connections: name%> etc.
IF you have not seen the before, The Take a look at this post on my blog.
You can build your owmbuilder class. The ExpressionBuilder Class Has One Method That Must Be Implement, GetCodeExpression.
................. ..
You can also override the ExpressionBuilder's EvaluteExpression method if you want your expression builder to be active on no-compile pages. This method most returns the results of the expression. If you implement the EvaluteExpression method, you must also override the SupportsEvaluate property, this property Will Return A Boolean Value, And Most Return True If The EvaluteExpression Method Should Be Used.
In The Example I'm Going to show you, The "My" Expression Prefix is used:
<% $ MyExpression%>
MYEXPRESSION IS The Expression That Is Going to Be Sent To The Value of The Expression.
The Following Code Is a Very Simple Custom Expression Builder That Will Only Return The Expression That IS Specified After The "My" prefix:
Using system.codedom; using system.componentmodel; using system.Web.Compilation;
Public class myexpressionBuilder: ExpressionBuilder {public myExpressionBuilder () {}
Public Static Object Getdata (String ExpRY Targettype, String EntryName) {return expression;}
public override object EvaluateExpression (object target, BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) {return MyExpressionBuilder.GetData (entry.Expression, target.GetType (), entry.Name);} public override CodeExpression GetCodeExpression (BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) {Type type1 = entry.DeclaringType; PropertyDescriptor descriptor1 = TypeDescriptor.GetProperties (type1) [entry.PropertyInfo.Name]; CodeExpression [] expressionArray1 = new CodeExpression [3]; expressionArray1 [0] = new CodePrimitiveExpression (entry.Expression .Trim ()); expressionArray1 [1] = new CodeTypeOfExpression (type1); expressionArray1 [2] = new CodePrimitiveExpression (entry.Name); return new CodeCastExpression (descriptor1.PropertyType, new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (base.GetType ()) , "GetData", expressionaray1);
Public override Bool Supportsevaluate {Return True;}}}
To use the custom expression Builder, you have to register it to the web.config in the
The expressionprefix attribute of the add element, is the name of the expression prefix:
<% $ expressionPREFIXNAME: Value%>
With the expressionprefix set to "my", The Expression Would Look Like this: <% $ my: value%>
The type attribute of the add element, is the type of the custom expression builder you have created. If you have added the custom expression builder to the / Code folder, you only need to write the name of the class. If you have created a Separated class library, you have to enter the offer name of the class and the assembly where the class.
Now Let's Take a look at the code for myExpressionBuilder.
First You NEED TO Add A Method That Will Return The Results for Your Expression, in This Example The getData method Will be used to return the results of the expression:
Public Static Object Getdata (String ExpRY Targettype, String EntryName) {return expression;}
The code above is used to return the results of the expression argument. In this case it will return the value of the expression. The arguments of the GetData method, will contain the value of the expression, the targetType is the type of where the expression is bounded to, for example a TextBox control etc. The last argument, entryName, is the name of the property where the expression is bounded to. You can for example us the targetType to check if your expression is allowed for the property where the expression IS specified.
As I mentioned earlier in my post, you can override the ExpressionBuilder's EvaluteExpression method if you want your expression builder to be active on no-compile pages The following code overrides the EvaluateExpression method and will call the GetData methoded described above.:
public override object EvaluateExpression (object target, BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) {return MyExpressionBuilder.GetData (entry.Expression, target.GetType (), entry.Name);} public override bool SupportsEvaluate {get {return true;} }
The EvaluteExpression Method Has Four Arguments, Target, Entry, PARESEDDATA AND CONTEXT. I WILL ONLY EXPLAIN THE ONE USED IN This Example:
Target
The Target Arguments Have The Type of The Type Where The Expression Is Bounded To.
Entry
THE Entry Argument Holds Information About Where The Expression IS Bounded, Such as The name of the property where the expression is bounded to and the value of the expression etc.
As you can see in the code above, the method will do a call to the GetData method where the expression (value), the type of target and the name of the property where the expression is bound to are passed as arguments. By default the SUPPORTEVALUTE PROPERTY WILL RETURN FALSE, SO TO USE The EvaluteExpression Method, The SupporteValuate Property Must Be Override and return True.
The next method to override is the GetCodeExpression This method returns a CodeExpression class It's a class with the information about the method that will return the results for the expression, in this case the GetData method:.. Public override CodeExpression GetCodeExpression (BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) {Type type1 = entry.DeclaringType; PropertyDescriptor descriptor1 = TypeDescriptor.GetProperties (type1) [entry.PropertyInfo.Name]; CodeExpression [] expressionArray1 = new CodeExpression [3]; expressionArray1 [0] = new CodePrimitiveExpression (entry .Expression.Trim ()); expressionArray1 [1] = new CodeTypeOfExpression (type1); expressionArray1 [2] = new CodePrimitiveExpression (entry.Name); return new CodeCastExpression (descriptor1.PropertyType, new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (base.GetType ( )), "GetData", expressionArray1));} The method Above will use the CodeCastExpression to specify a CodeExpression (The CodeCastExpression inherits the CodeExpression class). As you can see in the code above, I use the CodeCastExpression to specify the type of the property where the expression is bounded to, and the CodeTypeReferenceExpression to specify which method (GetData) that should be executed to get the results from for the expression. The argument to the GetData method is passed as a CodeExpression array where the items in the array are the value of the arguments that will be passed to the GetData method. I don't think Ir.
Here is an esple how you can users: