TemplateInstanceAttribute Kelas
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menentukan atribut metadata yang digunakan untuk menentukan jumlah instans templat yang diizinkan. Kelas ini tidak dapat diwariskan.
public ref class TemplateInstanceAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Property)]
public sealed class TemplateInstanceAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Property)>]
type TemplateInstanceAttribute = class
inherit Attribute
Public NotInheritable Class TemplateInstanceAttribute
Inherits Attribute
- Warisan
- Atribut
Contoh
Contoh kode berikut menunjukkan cara menggunakan TemplateInstance enumerasi dan TemplateInstanceAttribute kelas . Kontrol kustom LoginView bernama mengambil alih AnonymousTemplate properti dan menggunakan TemplateInstanceAttribute kelas untuk menentukan bahwa hanya satu instans properti yang AnonymousTemplate dibuat.MyLoginViewA
using System;
using System.Data;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Samples.AspNet.CS.Controls
{
public class MyLoginViewA : LoginView
{
private ITemplate _anonymoustemplate;
[Browsable(false),
DefaultValue(null),
PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(typeof(LoginView)),
TemplateInstance(TemplateInstance.Single)
]
public override ITemplate AnonymousTemplate
{
get
{
return _anonymoustemplate;
}
set
{
_anonymoustemplate = value;
}
}
}
}
Imports System.Data
Imports System.ComponentModel
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace Samples.AspNet.VB.Controls
Public Class MyLoginViewA
Inherits LoginView
Private _anonymoustemplate As ITemplate
<Browsable(False), DefaultValue(""), PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(GetType(LoginView)), TemplateInstance(TemplateInstance.Single)> _
Public Overrides Property AnonymousTemplate() As System.Web.UI.ITemplate
Get
Return _anonymoustemplate
End Get
Set(ByVal value As System.Web.UI.ITemplate)
_anonymoustemplate = value
End Set
End Property
End Class
End Namespace
Contoh kode berikut adalah file ASPX yang menggunakan MyLoginViewA kontrol dan menunjukkan cara mengakses properti TemplateInstanceAttribute kelas.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Register TagPrefix="AspNetSamples" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS.Controls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
// <Snippet3>
protected void Page_Load(object sender, EventArgs e)
{
// Get the class type for which to access metadata.
Type clsType = typeof(MyLoginViewA);
// Get the PropertyInfo object for FirstTemplate.
PropertyInfo pInfo = clsType.GetProperty("AnonymousTemplate");
// See if the TemplateInstanceAttribute is defined for this property.
bool isDef = Attribute.IsDefined(pInfo, typeof(TemplateInstanceAttribute));
// Display the result if the attribute exists.
if (isDef)
{
TemplateInstanceAttribute tia =
(TemplateInstanceAttribute)Attribute.GetCustomAttribute(pInfo, typeof(TemplateInstanceAttribute));
Response.Write("The <AnonymousTemplate> has the TemplateInstanceAttribute = " + tia.Instances.ToString() + ".<br />");
if (tia.IsDefaultAttribute())
Response.Write("The TemplateInstanceAttribute used is the same as the default instance.");
else
Response.Write("The TemplateInstanceAttribute used is not the same as the default instance.");
}
}
// </Snippet3>
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TemplateInstance Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<AspNetSamples:MyLoginViewA id="MyLoginViewA1" runat="server">
<AnonymousTemplate>
<asp:Label ID="LoginViewLabel1" runat="server" Text="LoginView Anonymous Template Text"/>
</AnonymousTemplate>
</AspNetSamples:MyLoginViewA>
</div>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Register TagPrefix="AspNetSamples" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB.Controls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
' <Snippet3>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' Get the class type for which to access metadata.
Dim clsType As Type = GetType(MyLoginViewA)
' Get the PropertyInfo object for FirstTemplate.
Dim pInfo As PropertyInfo = clsType.GetProperty("AnonymousTemplate")
' See if the TemplateInstanceAttribute is defined for this property.
Dim isDef As Boolean = Attribute.IsDefined(pInfo, GetType(TemplateContainerAttribute))
' Display the result if the attribute exists.
If isDef Then
Dim tia As TemplateInstanceAttribute = CType(Attribute.GetCustomAttribute(pInfo, GetType(TemplateInstanceAttribute)), TemplateInstanceAttribute)
Response.Write("The <AnonymousTemplate> has the TemplateInstanceAttribute = " & tia.Instances.ToString() & ".<br />")
If (tia.IsDefaultAttribute()) Then
Response.Write("The TemplateInstanceAttribute used is the same as the default instance.")
Else
Response.Write("The TemplateInstanceAttribute used is not the same as the default instance.")
End If
End If
End Sub
' </Snippet3>
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TemplateInstance Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<AspNetSamples:MyLoginViewA id="MyLoginViewA1" runat="server">
<AnonymousTemplate>
<asp:Label ID="LoginViewLabel1" runat="server" Text="LoginView Anonymous Template Text"/>
</AnonymousTemplate>
</AspNetSamples:MyLoginViewA>
</div>
</form>
</body>
</html>
Keterangan
Kelas ini TemplateInstanceAttribute memungkinkan Anda menandai properti templat sebagai properti yang memungkinkan instansiasi tunggal atau beberapa. Templat yang hanya memungkinkan satu instans dapat memiliki kontrol yang terkandung di dalamnya yang direferensikan. Properti ZoneTemplate adalah contoh properti yang hanya dapat diinstansiasi satu kali.
Kelas ini TemplateInstanceAttribute bersifat opsional. Jika properti templat tidak diperluas dengan TemplateInstanceAttribute kelas, nilai default, Multiple bidang , digunakan. Untuk informasi selengkapnya tentang menggunakan atribut, lihat Atribut.
Konstruktor
| Nama | Deskripsi |
|---|---|
| TemplateInstanceAttribute(TemplateInstance) |
Menginisialisasi instans TemplateInstanceAttribute baru kelas dengan nilai enumerasi yang ditentukan TemplateInstance . |
Bidang
| Nama | Deskripsi |
|---|---|
| Default |
Menentukan nilai default untuk TemplateInstanceAttribute kelas . Bidang ini hanya dapat dibaca. |
| Multiple |
Membuat instans TemplateInstanceAttribute kelas sebagai instans yang mewakili templat yang akan diinstansiasi beberapa kali. Bidang ini hanya dapat dibaca. |
| Single |
Membuat instans TemplateInstanceAttribute kelas sebagai instans yang mewakili templat yang akan diinstansiasi satu kali. Bidang ini hanya dapat dibaca. |
Properti
| Nama | Deskripsi |
|---|---|
| Instances |
TemplateInstance Mendapatkan nilai enumerasi yang diwakili instans templat saat ini. |
| TypeId |
Ketika diimplementasikan dalam kelas turunan, mendapatkan pengidentifikasi unik untuk Attributeini. (Diperoleh dari Attribute) |
Metode
| Nama | Deskripsi |
|---|---|
| Equals(Object) |
Menunjukkan apakah objek yang ditentukan adalah TemplateInstanceAttribute objek dan identik dengan objek ini TemplateInstanceAttribute . |
| GetHashCode() |
Mendapatkan kode hash untuk objek ini TemplateInstanceAttribute . |
| GetType() |
Mendapatkan Type instans saat ini. (Diperoleh dari Object) |
| IsDefaultAttribute() |
Mengembalikan nilai yang menunjukkan apakah objek saat ini TemplateInstanceAttribute sama dengan objek default TemplateInstanceAttribute . |
| Match(Object) |
Saat ditimpa dalam kelas turunan, mengembalikan nilai yang menunjukkan apakah instans ini sama dengan objek tertentu. (Diperoleh dari Attribute) |
| MemberwiseClone() |
Membuat salinan dangkal dari Objectsaat ini. (Diperoleh dari Object) |
| ToString() |
Mengembalikan string yang mewakili objek saat ini. (Diperoleh dari Object) |
Implementasi Antarmuka Eksplisit
| Nama | Deskripsi |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Memetakan sekumpulan nama ke sekumpulan pengidentifikasi pengiriman yang sesuai. (Diperoleh dari Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Mengambil informasi jenis untuk objek, yang dapat digunakan untuk mendapatkan informasi jenis untuk antarmuka. (Diperoleh dari Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Mengambil jumlah antarmuka informasi jenis yang disediakan objek (baik 0 atau 1). (Diperoleh dari Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Menyediakan akses ke properti dan metode yang diekspos oleh objek. (Diperoleh dari Attribute) |