Udostępnij za pośrednictwem


ExpressionPrefixAttribute Klasa

Definicja

Określa atrybut prefiksu do użycia dla konstruktora wyrażeń. Tej klasy nie można dziedziczyć.

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ę przekazywaną do wyrażenia. Aby uruchomić ten przykład, musisz najpierw zarejestrować konstruktora wyrażeń niestandardowych w pliku Web.config. W pierwszym przykładzie kodu pokazano, 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" runat="server"
Text="<%$ MyCustomExpression:Hello, world! %>" />

W trzecim przykładzie kodu pokazano, jak opracować dostosowany konstruktor wyrażeń, wyprowadzając element z ExpressionBuilder. Aby uruchomić ten przykładowy kod, 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. ExpressionPrefix Użyj właściwości , aby uzyskać nazwę prefiksu skojarzonego z obiektemExpressionPrefixAttribute. Konstruktor wyrażeń wyszukuje dowolne instrukcje z następującym formularzem:

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

Następnie konstruktor wyrażeń na podstawie prefiksu wyrażenia generuje kod 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

Nazwa Opis
ExpressionPrefixAttribute(String)

Inicjuje nowe wystąpienie klasy ExpressionPrefixAttribute.

Właściwości

Nazwa Opis
ExpressionPrefix

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

TypeId

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

(Odziedziczone po Attribute)

Metody

Nazwa Opis
Equals(Object)

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

(Odziedziczone po Attribute)
GetHashCode()

Zwraca kod skrótu dla tego wystąpienia.

(Odziedziczone po Attribute)
GetType()

Pobiera Type bieżącego wystąpienia.

(Odziedziczone po Object)
IsDefaultAttribute()

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

(Odziedziczone po Attribute)
Match(Object)

Po zastąpieniu 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 Object.

(Odziedziczone po Object)
ToString()

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

(Odziedziczone po Object)

Jawne implementacje interfejsu

Nazwa Opis
_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 obiektu, którego można użyć do uzyskania 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)

Zapewnia dostęp do właściwości i metod uwidocznionych przez obiekt.

(Odziedziczone po Attribute)

Dotyczy

Zobacz też