다음을 통해 공유


ConstructorBuilder.AddDeclarativeSecurity 메서드

정의

이 생성자에 선언적 보안을 추가합니다.

public:
 void AddDeclarativeSecurity(System::Security::Permissions::SecurityAction action, System::Security::PermissionSet ^ pset);
public void AddDeclarativeSecurity (System.Security.Permissions.SecurityAction action, System.Security.PermissionSet pset);
member this.AddDeclarativeSecurity : System.Security.Permissions.SecurityAction * System.Security.PermissionSet -> unit
Public Sub AddDeclarativeSecurity (action As SecurityAction, pset As PermissionSet)

매개 변수

action
SecurityAction

요청, 어설션 등과 같은 수행할 보안 작업입니다.

pset
PermissionSet

작업이 적용되는 사용 권한 집합입니다.

예외

action이 올바르지 않습니다(RequestMinimum, RequestOptional 및 RequestRefuse가 유효하지 않음).

포함하는 형식은 CreateType()을 사용하여 이전에 만든 것입니다.

또는

사용 권한 집합 pset에 이전에 AddDeclarativeSecurity에 의해 추가된 작업이 포함되어 있습니다.

pset이(가) null인 경우

예제

다음 코드 샘플에서는 의 AddDeclarativeSecurity사용을 보여 줍니다.

MethodBuilder^ myMethodBuilder = nullptr;

AppDomain^ myCurrentDomain = AppDomain::CurrentDomain;
// Create assembly in current CurrentDomain
AssemblyName^ myAssemblyName = gcnew AssemblyName;
myAssemblyName->Name = "TempAssembly";
// Create a dynamic assembly
myAssemblyBuilder = myCurrentDomain->DefineDynamicAssembly(
   myAssemblyName, AssemblyBuilderAccess::RunAndSave );
// Create a dynamic module in the assembly.
myModuleBuilder = myAssemblyBuilder->DefineDynamicModule( "TempModule" );
FieldInfo^ myFieldInfo =
   myModuleBuilder->DefineUninitializedData( "myField", 2, FieldAttributes::Public );
// Create a type in the module
TypeBuilder^ myTypeBuilder = myModuleBuilder->DefineType( "TempClass", TypeAttributes::Public );
FieldBuilder^ myGreetingField = myTypeBuilder->DefineField( "Greeting",
   String::typeid, FieldAttributes::Public );
array<Type^>^myConstructorArgs = {String::typeid};
// Define a constructor of the dynamic class.
ConstructorBuilder^ myConstructor = myTypeBuilder->DefineConstructor(
   MethodAttributes::Public, CallingConventions::Standard, myConstructorArgs );
PermissionSet^ myPset = gcnew PermissionSet( PermissionState::Unrestricted );
// Add declarative security to the constructor.
Console::WriteLine( "Adding declarative security to the constructor....." );
Console::WriteLine( "The Security action to be taken is \"DENY\" and" +
   " Permission set is \"UNRESTRICTED\"." );
myConstructor->AddDeclarativeSecurity( SecurityAction::Deny, myPset );
MethodAttributes myMethodAttributes = myConstructor->Attributes;
Type^ myAttributeType = MethodAttributes::typeid;
int myAttribValue = (int)myMethodAttributes;
if (  !myAttributeType->IsEnum )
{
   Console::WriteLine( "This is not an Enum" );
}
array<FieldInfo^>^myFieldInfo1 = myAttributeType->GetFields( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Static) );
Console::WriteLine( "The Field info names of the Attributes for the constructor are:" );
for ( int i = 0; i < myFieldInfo1->Length; i++ )
{
   int myFieldValue =  *dynamic_cast<Int32^>(myFieldInfo1[ i ]->GetValue( nullptr ));
   if ( (myFieldValue & myAttribValue) == myFieldValue )
   {
      Console::WriteLine( "   {0}", myFieldInfo1[ i ]->Name );
   }
}

Type^ myType2 = myConstructor->DeclaringType;
Console::WriteLine( "The declaring type is : {0}", myType2 );
ParameterBuilder^ myParameterBuilder1 =
   myConstructor->DefineParameter( 1, ParameterAttributes::Out, "My Parameter Name1" );
Console::WriteLine( "The name of the parameter is : {0}",
   myParameterBuilder1->Name );
if ( myParameterBuilder1->IsIn )
      Console::WriteLine( "{0} is Input parameter.", myParameterBuilder1->Name );
else
      Console::WriteLine( "{0} is not Input Parameter.", myParameterBuilder1->Name );
ParameterBuilder^ myParameterBuilder2 =
   myConstructor->DefineParameter( 1, ParameterAttributes::In, "My Parameter Name2" );
Console::WriteLine( "The Parameter name is : {0}",
   myParameterBuilder2->Name );
if ( myParameterBuilder2->IsIn )
      Console::WriteLine( "{0} is Input parameter.", myParameterBuilder2->Name );
else
      Console::WriteLine( "{0} is not Input Parameter.", myParameterBuilder2->Name );
MethodBuilder myMethodBuilder=null;

AppDomain myCurrentDomain = AppDomain.CurrentDomain;
// Create assembly in current CurrentDomain
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "TempAssembly";
// Create a dynamic assembly
myAssemblyBuilder = myCurrentDomain.DefineDynamicAssembly
   (myAssemblyName, AssemblyBuilderAccess.RunAndSave);
// Create a dynamic module in the assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule");
FieldInfo myFieldInfo =
   myModuleBuilder.DefineUninitializedData("myField",2,FieldAttributes.Public);
// Create a type in the module
TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("TempClass",TypeAttributes.Public);
FieldBuilder myGreetingField = myTypeBuilder.DefineField("Greeting",
   typeof(String), FieldAttributes.Public);
Type[] myConstructorArgs = { typeof(String) };
// Define a constructor of the dynamic class.
ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
   MethodAttributes.Public, CallingConventions.Standard, myConstructorArgs);
PermissionSet myPset = new PermissionSet(PermissionState.Unrestricted);
// Add declarative security to the constructor.
Console.WriteLine("Adding declarative security to the constructor.....");
Console.WriteLine("The Security action to be taken is \"DENY\" and" +
   " Permission set is \"UNRESTRICTED\".");
myConstructor.AddDeclarativeSecurity(SecurityAction.Deny,myPset);
MethodAttributes myMethodAttributes = myConstructor.Attributes;
Type myAttributeType = typeof(MethodAttributes);
int myAttribValue = (int) myMethodAttributes;
if(! myAttributeType.IsEnum)
{
   Console.WriteLine("This is not an Enum");
}
FieldInfo[] myFieldInfo1 = myAttributeType.GetFields(BindingFlags.Public | BindingFlags.Static);
Console.WriteLine("The Field info names of the Attributes for the constructor are:");
for (int i = 0; i < myFieldInfo1.Length; i++)
{
   int myFieldValue = (Int32)myFieldInfo1[i].GetValue(null);
   if ((myFieldValue & myAttribValue) == myFieldValue)
   {
      Console.WriteLine("   " + myFieldInfo1[i].Name);
   }
}

Type myType2 = myConstructor.DeclaringType;
Console.WriteLine("The declaring type is : "+myType2.ToString());
ParameterBuilder myParameterBuilder1 =
   myConstructor.DefineParameter(1,  ParameterAttributes.Out, "My Parameter Name1");
Console.WriteLine("The name of the parameter is : " +
   myParameterBuilder1.Name);
if(myParameterBuilder1.IsIn)
   Console.WriteLine(myParameterBuilder1.Name +" is Input parameter.");
else
   Console.WriteLine(myParameterBuilder1.Name +" is not Input Parameter.");
ParameterBuilder myParameterBuilder2 =
   myConstructor.DefineParameter(1, ParameterAttributes.In, "My Parameter Name2");
Console.WriteLine("The Parameter name is : " +
   myParameterBuilder2.Name);
if(myParameterBuilder2.IsIn)
   Console.WriteLine(myParameterBuilder2.Name +" is Input parameter.");
else
   Console.WriteLine(myParameterBuilder2.Name + " is not Input Parameter.");
Dim myMethodBuilder As MethodBuilder = Nothing

Dim myCurrentDomain As AppDomain = AppDomain.CurrentDomain
' Create assembly in current CurrentDomain
Dim myAssemblyName As New AssemblyName()
myAssemblyName.Name = "TempAssembly"
' Create a dynamic assembly
myAssemblyBuilder = _
      myCurrentDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave)
' Create a dynamic module in the assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule")
Dim myFieldInfo As FieldInfo = _
    myModuleBuilder.DefineUninitializedData("myField", 2, FieldAttributes.Public)
' Create a type in the module
Dim myTypeBuilder As TypeBuilder = _
    myModuleBuilder.DefineType("TempClass", TypeAttributes.Public)
Dim myGreetingField As FieldBuilder = _
   myTypeBuilder.DefineField("Greeting", GetType(String), FieldAttributes.Public)
Dim myConstructorArgs As Type() = {GetType(String)}
' Define a constructor of the dynamic class.
Dim myConstructor As ConstructorBuilder = _
    myTypeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, _
                                    myConstructorArgs)
Dim myPset As New PermissionSet(PermissionState.Unrestricted)
' Add declarative security to the constructor.
Console.WriteLine("Adding declarative security to the constructor.....")
Console.WriteLine("The Security action to be taken is ""DENY"" and" + _
                  " Permission set is ""UNRESTRICTED"".")
myConstructor.AddDeclarativeSecurity(SecurityAction.Deny, myPset)
Dim myMethodAttributes As MethodAttributes = myConstructor.Attributes
Dim myAttributeType As Type = GetType(MethodAttributes)
Dim myAttribValue As Integer = CInt(myMethodAttributes)
If Not myAttributeType.IsEnum Then
   Console.WriteLine("This is not an Enum")
End If
Dim myFieldInfo1 As FieldInfo() = myAttributeType.GetFields(BindingFlags.Public Or _
                                                             BindingFlags.Static)
Console.WriteLine("The Field info names of the Attributes for the constructor are:")
Dim i As Integer
For i = 0 To myFieldInfo1.Length - 1
   Dim myFieldValue As Integer = CType(myFieldInfo1(i).GetValue(Nothing), Int32)
   If(myFieldValue And myAttribValue) = myFieldValue Then
      Console.WriteLine("   " + myFieldInfo1(i).Name)
   End If
Next i

Dim myType2 As Type = myConstructor.DeclaringType
Console.WriteLine("The declaring type is : " + myType2.ToString())
Dim myParameterBuilder1 As ParameterBuilder = _
    myConstructor.DefineParameter(1, ParameterAttributes.Out, "My Parameter Name1")
Console.WriteLine("The name of the parameter is : " + myParameterBuilder1.Name)
If myParameterBuilder1.IsIn Then
   Console.WriteLine(myParameterBuilder1.Name + " is Input parameter.")
Else
   Console.WriteLine(myParameterBuilder1.Name + " is not Input Parameter.")
End If
Dim myParameterBuilder2 As ParameterBuilder = _
    myConstructor.DefineParameter(1, ParameterAttributes.In, "My Parameter Name2")
Console.WriteLine("The Parameter name is : " + myParameterBuilder2.Name)
If myParameterBuilder2.IsIn Then
   Console.WriteLine(myParameterBuilder2.Name + " is Input parameter.")
Else
   Console.WriteLine(myParameterBuilder2.Name + " is not Input Parameter.")
End If

설명

AddDeclarativeSecurity 은 각 호출에서 보안 작업(예: Demand, Assert및 )과 Deny작업이 적용되는 권한 집합을 지정하여 여러 번 호출할 수 있습니다.

참고

.NET Framework 버전 1.0, 1.1 및 2.0에서는 이 메서드를 사용하여 생성자에 적용된 선언적 보안 특성이 이전 XML 메타데이터 형식으로 저장됩니다. 선언적 보안 특성 내보내기를 참조하세요.

적용 대상