ExpressionPrefixAttribute Klasa

Definicja

Określa atrybut prefiksu do użycia dla konstruktora wyrażeń. Klasa ta nie może być dziedziczona.

public ref class ExpressionPrefixAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false)]
public sealed class ExpressionPrefixAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false)>]
type ExpressionPrefixAttribute = class
    inherit Attribute
Public NotInheritable Class ExpressionPrefixAttribute
Inherits Attribute
Dziedziczenie
ExpressionPrefixAttribute
Atrybuty

Przykłady

W poniższych przykładach kodu pokazano, jak używać ExpressionPrefixAttribute klasy. Atrybut jest stosowany do konstruktora wyrażeń niestandardowych, który implementuje klasę abstrakcyjną ExpressionBuilder . Ta implementacja ExpressionBuilder zwraca obliczoną instrukcję, która jest przekazywana do wyrażenia. Aby uruchomić ten przykład, musisz najpierw zarejestrować konstruktora wyrażeń niestandardowych w pliku Web.config. Pierwszy przykład kodu pokazuje, jak zarejestrować konstruktora wyrażeń niestandardowych w pliku Web.config.

<configuration>  
    <system.web>  
       <compilation>  
          <expressionBuilders>  
              <add expressionPrefix="MyCustomExpression"  
               type="MyCustomExpressionBuilder"/>  
          </expressionBuilders>  
       </compilation>  
    </system.web>  
</configuration>  

Drugi przykład kodu pokazuje, jak odwoływać się do wyrażenia w pliku aspx.

<asp:Label ID="Label1&quot; runat=&quot;server"   
Text="<%$ MyCustomExpression:Hello, world! %>" />  

Trzeci przykład kodu pokazuje, jak utworzyć dostosowany konstruktor wyrażeń przez wyprowadzenie z ExpressionBuilderklasy . Aby uruchomić ten przykład kodu, należy umieścić klasę w folderze App_Code.

using System;
using System.CodeDom;
using System.Web.UI;
using System.ComponentModel;
using System.Web.Compilation;
using System.Web.UI.Design;

// Apply ExpressionEditorAttributes to allow the 
// expression to appear in the designer.
[ExpressionPrefix("MyCustomExpression")]
[ExpressionEditor("MyCustomExpressionEditor")]
public class MyExpressionBuilder : ExpressionBuilder
{
    // Create a method that will return the result 
    // set for the expression argument.
    public static object GetEvalData(string expression, Type target, string entry)
    {
        return expression;
    }

    public override object EvaluateExpression(object target, BoundPropertyEntry entry, 
    object parsedData, ExpressionBuilderContext context)
    {
        return GetEvalData(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()), "GetEvalData", expressionArray1));
    }

    public override bool SupportsEvaluate
    {
        get { return true; }
    }
}
Imports System.CodeDom
Imports System.Web.UI
Imports System.ComponentModel
Imports System.Web.Compilation
Imports System.Web.UI.Design

' Apply ExpressionEditorAttributes to allow the 
' expression to appear in the designer.
<ExpressionPrefix("MyCustomExpression")> _
<ExpressionEditor("MyCustomExpressionEditor")> _
Public Class MyExpressionBuilder
    Inherits ExpressionBuilder
    ' Create a method that will return the result 
    ' set for the expression argument.
    Public Shared Function GetEvalData(ByVal expression As String, _
       ByVal target As Type, ByVal entry As String) As Object
        Return expression
    End Function

    Public Overrides Function EvaluateExpression(ByVal target As Object, _
       ByVal entry As BoundPropertyEntry, ByVal parsedData As Object, _
       ByVal context As ExpressionBuilderContext) As Object
        Return GetEvalData(entry.Expression, target.GetType(), entry.Name)
    End Function

    Public Overrides Function GetCodeExpression(ByVal entry _
       As BoundPropertyEntry, ByVal parsedData As Object, ByVal context _
       As ExpressionBuilderContext) As CodeExpression
        Dim type1 As Type = entry.DeclaringType
        Dim descriptor1 As PropertyDescriptor = _
           TypeDescriptor.GetProperties(type1)(entry.PropertyInfo.Name)
        Dim expressionArray1(2) As CodeExpression
        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 _
           (MyBase.GetType()), "GetEvalData", expressionArray1))
    End Function

    Public Overrides ReadOnly Property SupportsEvaluate() As Boolean
        Get
            Return True
        End Get
    End Property
End Class

Uwagi

Klasa ExpressionPrefixAttribute jest używana w czasie projektowania z wyrażeniami, które nie są zdefiniowane w pliku konfiguracji. Użyj właściwości , ExpressionPrefix aby uzyskać nazwę prefiksu skojarzonego z obiektem ExpressionPrefixAttribute . Konstruktor wyrażeń szuka dowolnych instrukcji z następującym formularzem:

<%$ [expressionPrefix]:[expressionValue] %>

Następnie konstruktor wyrażeń, na podstawie prefiksu wyrażenia, generuje kod dla przypisania właściwości. Parametr expressionPrefix odwołuje się do skonfigurowanego konstruktora wyrażeń, który jest zdefiniowany w pliku konfiguracji lub za pośrednictwem ExpressionPrefixAttribute obiektu.

Konstruktory

ExpressionPrefixAttribute(String)

Inicjuje nowe wystąpienie klasy ExpressionPrefixAttribute.

Właściwości

ExpressionPrefix

Pobiera wartość prefiksu dla bieżącego ExpressionBuilder obiektu.

TypeId

Po zaimplementowaniu w klasie pochodnej pobiera unikatowy identyfikator dla tego Attributeelementu .

(Odziedziczone po Attribute)

Metody

Equals(Object)

Zwraca wartość wskazującą, czy to wystąpienie jest równe podanemu obiektowi.

(Odziedziczone po Attribute)
GetHashCode()

Zwraca wartość skrótu dla tego wystąpienia.

(Odziedziczone po Attribute)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
IsDefaultAttribute()

W przypadku zastąpienia w klasie pochodnej wskazuje, czy wartość tego wystąpienia jest wartością domyślną klasy pochodnej.

(Odziedziczone po Attribute)
Match(Object)

Po przesłonięciu w klasie pochodnej zwraca wartość wskazującą, czy to wystąpienie jest równe określonemu obiektowi.

(Odziedziczone po Attribute)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Jawne implementacje interfejsu

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Zestaw nazw jest mapowany na odpowiedni zestaw identyfikatorów wysyłania.

(Odziedziczone po Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Pobiera informacje o typie dla obiektu, który może służyć do pobierania informacji o typie dla interfejsu.

(Odziedziczone po Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Pobiera informację o liczbie typów interfejsów, jakie zawiera obiekt (0 lub 1).

(Odziedziczone po Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Umożliwia dostęp do właściwości i metod udostępnianych przez obiekt.

(Odziedziczone po Attribute)

Dotyczy

Zobacz też