WebPartTransformerAttribute(Type, Type) 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 WebPartTransformerAttribute 類別的新執行個體。
public:
WebPartTransformerAttribute(Type ^ consumerType, Type ^ providerType);
public WebPartTransformerAttribute (Type consumerType, Type providerType);
new System.Web.UI.WebControls.WebParts.WebPartTransformerAttribute : Type * Type -> System.Web.UI.WebControls.WebParts.WebPartTransformerAttribute
Public Sub New (consumerType As Type, providerType As Type)
參數
例外狀況
consumerType
或 providerType
未指定。
consumerType
等於 providerType
。
範例
下列程式碼範例示範如何搭配自訂 WebPartTransformer 類別使用 WebPartTransformerAttribute 屬性。 屬性工作表示 RowToStringTransformer
類別可以將 型別 IWebPartRow
的提供者連接點轉換為 型 IString
別的取用者連接點。
此程式碼範例是類別概觀中找到較大範例的 WebPartTransformer 一部分。
// A transformer that transforms a row to a string.
[AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal)]
[WebPartTransformer(typeof(IWebPartRow), typeof(IString))]
public class RowToStringTransformer : WebPartTransformer, IString
{
private IWebPartRow _provider;
private StringCallback _callback;
private void GetRowData(object rowData)
{
PropertyDescriptorCollection props = _provider.Schema;
if (props != null && props.Count > 0 && rowData != null)
{
string returnValue = String.Empty;
foreach (PropertyDescriptor prop in props)
{
if (prop != props[0])
{
returnValue += ", ";
}
returnValue += prop.DisplayName + ": " + prop.GetValue(rowData);
}
_callback(returnValue);
}
else
{
_callback(null);
}
}
public override object Transform(object providerData)
{
_provider = (IWebPartRow)providerData;
return this;
}
void IString.GetStringValue(StringCallback callback)
{
if (callback == null)
{
throw new ArgumentNullException("callback");
}
if (_provider != null)
{
_callback = callback;
_provider.GetRowData(new RowCallback(GetRowData));
}
else
{
callback(null);
}
}
}
' A transformer that transforms a row to a string.
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
<WebPartTransformer(GetType(IWebPartRow), GetType(IString))> _
Public Class RowToStringTransformer
Inherits WebPartTransformer
Implements IString
Private _provider As IWebPartRow
Private _callback As StringCallback
Private Sub GetRowData(ByVal rowData As Object)
Dim props As PropertyDescriptorCollection = _provider.Schema
If ((Not (props Is Nothing)) AndAlso (props.Count > 0) _
AndAlso (Not (rowData Is Nothing))) Then
Dim returnValue As String = String.Empty
For Each prop As PropertyDescriptor In props
If Not (prop Is props(0)) Then
returnValue += ", "
End If
returnValue += prop.DisplayName.ToString() + ": " + _
prop.GetValue(rowData).ToString()
Next
_callback(returnValue)
Else
_callback(Nothing)
End If
End Sub
Public Overrides Function Transform(ByVal providerData As Object) As Object
_provider = CType(providerData, IWebPartRow)
Return Me
End Function
Sub GetStringValue(ByVal callback As StringCallback) _
Implements IString.GetStringValue
If (callback Is Nothing) Then
Throw New ArgumentNullException("callback")
End If
If (Not (_provider Is Nothing)) Then
_callback = callback
_provider.GetRowData(New RowCallback(AddressOf GetRowData))
Else
callback(Nothing)
End If
End Sub
End Class
備註
建 WebPartTransformerAttribute 構函式會設定取用者連接點類型和轉換器的提供者連接點類型。 您可以分別透過 ConsumerType 屬性和 ProviderType 屬性來擷取這些值。