WebPartTransformerAttribute(Type, Type) 构造函数
定义
初始化 WebPartTransformerAttribute 类的新实例。Initializes a new instance of the WebPartTransformerAttribute class.
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
- Type
使用者连接点所支持的接口的 Type。The Type of the interface supported by the consumer connection point.
- providerType
- Type
提供者连接点所支持的接口的 Type。The Type of the interface supported by the provider connection point.
例外
未指定 consumerType 或 providerType。consumerType or providerType is not specified.
consumerType 等于 providerType。consumerType equals providerType.
示例
下面的代码示例演示如何将 WebPartTransformerAttribute 属性与自定义类结合使用 WebPartTransformer 。The following code example demonstrates the use of a WebPartTransformerAttribute attribute with a customized WebPartTransformer class. 特性指示 RowToStringTransformer 类可以将类型的提供程序连接点转换 IWebPartRow 为类型的使用者连接点 IString 。The attribute indicates that the RowToStringTransformer class can transform a provider connection point of type IWebPartRow to a consumer connection point of type IString.
此代码示例摘自类概述中的一个更大的示例 WebPartTransformer 。This code example is part of a larger example found in the WebPartTransformer class overview.
// 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构造函数为转换器设置使用者连接点类型和提供程序连接点类型。The WebPartTransformerAttribute constructor sets the consumer connection point type and the provider connection point type for the transformer. 这些值可 ConsumerType 分别通过属性和属性进行检索 ProviderType 。These values can be retrieved through the ConsumerType property and the ProviderType property, respectively.