Type.IsContextfulImpl Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Implementeert de IsContextful eigenschap en bepaalt of de Type eigenschap in een context kan worden gehost.
protected:
virtual bool IsContextfulImpl();
protected virtual bool IsContextfulImpl();
abstract member IsContextfulImpl : unit -> bool
override this.IsContextfulImpl : unit -> bool
Protected Overridable Function IsContextfulImpl () As Boolean
Retouren
trueals de Type kan worden gehost in een context; anders. false
Voorbeelden
In het volgende voorbeeld ziet u een gebruik van de IsContextfulImpl methode.
using System;
using System.Reflection;
public class MyTypeDelegatorClass : TypeDelegator
{
public string myElementType = null;
private Type myType = null ;
public MyTypeDelegatorClass(Type myType) : base(myType)
{
this.myType = myType;
}
// Override IsContextfulImpl.
protected override bool IsContextfulImpl()
{
// Check whether the type is contextful.
if(myType.IsContextful)
{
myElementType = " is contextful ";
return true;
}
return false;
}
}
public class MyTypeDemoClass
{
public static void Main()
{
try
{
MyTypeDelegatorClass myType;
Console.WriteLine ("Check whether MyContextBoundClass can be hosted in a context.");
// Check whether MyContextBoundClass is contextful.
myType = new MyTypeDelegatorClass(typeof(MyContextBoundClass));
if( myType.IsContextful)
{
Console.WriteLine(typeof(MyContextBoundClass) + " can be hosted in a context.");
}
else
{
Console.WriteLine(typeof(MyContextBoundClass) + " cannot be hosted in a context.");
}
// Check whether the int type is contextful.
myType = new MyTypeDelegatorClass(typeof(MyTypeDemoClass));
Console.WriteLine ("\nCheck whether MyTypeDemoClass can be hosted in a context.");
if( myType.IsContextful)
{
Console.WriteLine(typeof(MyTypeDemoClass) + " can be hosted in a context.");
}
else
{
Console.WriteLine(typeof(MyTypeDemoClass) + " cannot be hosted in a context.");
}
}
catch( Exception e )
{
Console.WriteLine("Exception: {0}", e.Message);
}
}
}
// This class demonstrates IsContextfulImpl.
public class MyContextBoundClass : ContextBoundObject
{
public string myString = "This class is used to demonstrate members of the Type class.";
}
open System
open System.Reflection
type MyTypeDelegatorClass(myType) =
inherit TypeDelegator(myType)
member val myElementType = null with get, set
// Override IsContextfulImpl.
override this.IsContextfulImpl() =
// Check whether the type is contextful.
if myType.IsContextful then
this.myElementType <- " is contextful "
true
else false
// This class demonstrates IsContextfulImpl.
type MyContextBoundClass() =
inherit ContextBoundObject()
let myString = "This class is used to demonstrate members of the Type class."
type MyTypeDemoClass() = class end
try
printfn "Check whether MyContextBoundClass can be hosted in a context."
// Check whether MyContextBoundClass is contextful.
let myType = MyTypeDelegatorClass typeof<MyContextBoundClass>
if myType.IsContextful then
printfn $"{typeof<MyContextBoundClass>} can be hosted in a context."
else
printfn $"{typeof<MyContextBoundClass>} cannot be hosted in a context."
// Check whether the int type is contextful.
let myType = MyTypeDelegatorClass typeof<MyTypeDemoClass>
printfn "\nCheck whether MyTypeDemoClass can be hosted in a context."
if myType.IsContextful then
printfn $"{typeof<MyTypeDemoClass>} can be hosted in a context."
else
printfn $"{typeof<MyTypeDemoClass>} cannot be hosted in a context."
with e ->
printfn $"Exception: {e.Message}"
Imports System.Reflection
Public Class MyTypeDelegatorClass
Inherits TypeDelegator
Public myElementType As String = Nothing
Private myType As Type = Nothing
Public Sub New(ByVal myType As Type)
MyBase.New(myType)
Me.myType = myType
End Sub
' Override IsContextfulImpl.
Protected Overrides Function IsContextfulImpl() As Boolean
' Check whether the type is contextful.
If myType.IsContextful Then
myElementType = " is contextful "
Return True
End If
Return False
End Function 'IsContextfulImpl
End Class
Public Class MyTypeDemoClass
Public Shared Sub Main()
Try
Dim myType As MyTypeDelegatorClass
Console.WriteLine("Check whether MyContextBoundClass can be hosted in a context.")
' Check whether MyContextBoundClass is contextful.
myType = New MyTypeDelegatorClass(GetType(MyContextBoundClass))
If myType.IsContextful Then
Console.WriteLine(GetType(MyContextBoundClass).ToString() + " can be hosted in a context.")
Else
Console.WriteLine(GetType(MyContextBoundClass).ToString() + " cannot be hosted in a context.")
End If
' Check whether the int type is contextful.
myType = New MyTypeDelegatorClass(GetType(MyTypeDemoClass))
Console.WriteLine(ControlChars.NewLine + "Check whether MyTypeDemoClass can be hosted in a context.")
If myType.IsContextful Then
Console.WriteLine(GetType(MyTypeDemoClass).ToString() + " can be hosted in a context.")
Else
Console.WriteLine(GetType(MyTypeDemoClass).ToString() + " cannot be hosted in a context.")
End If
Catch e As Exception
Console.WriteLine("Exception: {0}", e.Message.ToString())
End Try
End Sub
End Class
' This class demonstrates the IsContextfulImpl method.
Public Class MyContextBoundClass
Inherits ContextBoundObject
Public myString As String = "This class is used to demonstrate members of the Type class."
End Class
Opmerkingen
Deze methode kan worden overschreven door een afgeleide klasse.
Een context onderschept aanroepen naar de klasseleden en dwing beleidsregels af die worden toegepast op de klasse, zoals synchronisatie.