Прочитать на английском

Поделиться через


ConstructorBuilder.AddDeclarativeSecurity Метод

Определение

Добавляет декларативную безопасность в этот конструктор.

public void AddDeclarativeSecurity(System.Security.Permissions.SecurityAction action, System.Security.PermissionSet pset);

Параметры

action
SecurityAction

Выполняемое действие безопасности, например Demand, Assert и т. д.

pset
PermissionSet

Набор разрешений, к которому применяется действие.

Исключения

action является недопустимым (RequestMinimum, RequestOptional и RequestRefuse являются недопустимыми).

Содержащий тип был создан ранее с помощью CreateType().

-или-

Набор разрешений pset содержит действие, добавленное ранее с помощью AddDeclarativeSecurity.

pset имеет значение null.

Примеры

В следующем примере кода показано использование AddDeclarativeSecurity.

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.");

Комментарии

AddDeclarativeSecurity может вызываться несколько раз, при этом каждый вызов указывает действие безопасности (например Demand, , Assertи Deny) и набор разрешений, к которым применяется действие.

Примечание

В платформа .NET Framework версий 1.0, 1.1 и 2.0 декларативные атрибуты безопасности, применяемые к конструктору с помощью этого метода, хранятся в старом формате XML-метаданных. См. раздел Создание декларативных атрибутов безопасности.

Применяется к

Продукт Версии
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1