Export (0) Print
Expand All

ResourcePermissionBase Class

Allows control of code access security permissions.

For a list of all members of this type, see ResourcePermissionBase Members.

System.Object
   System.Security.CodeAccessPermission
      System.Security.Permissions.ResourcePermissionBase
         System.Diagnostics.EventLogPermission
         System.Diagnostics.PerformanceCounterPermission
         System.DirectoryServices.DirectoryServicesPermission
         System.ServiceProcess.ServiceControllerPermission

[Visual Basic]
<Serializable>
MustInherit Public Class ResourcePermissionBase
   Inherits CodeAccessPermission
   Implements IUnrestrictedPermission
[C#]
[Serializable]
public abstract class ResourcePermissionBase :
   CodeAccessPermission, IUnrestrictedPermission
[C++]
[Serializable]
public __gc __abstract class ResourcePermissionBase : public
   CodeAccessPermission, IUnrestrictedPermission
[JScript]
public
   Serializable
abstract class ResourcePermissionBase extends
   CodeAccessPermission implements IUnrestrictedPermission

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

Notes to Inheritors:  When you inherit from ResourcePermissionBase, you must provide at least three constructors, set two properties, and provide a third property. The required constructors are: a default constructor, one that takes a PermissionState as a parameter, and as many as needed that take values for the properties. The properties that need to be set are PermissionAccessType and TagNames. The third property that is needed is one that returns the permission entries. For an example of an implementation of this class, see PerformanceCounterPermission. In PerformanceCounterPermission, the TagNames property is set privately to "Machine" and "Category", the PermissionAccessType property is privately set to the type of PerformanceCounterPermissionAccess, and the PermissionEntries property returns the permission entries.

Example

[Visual Basic, C#, C++] The following example inherits from the ResourcePermissionBase class. 4 kinds of constructors are implemented. The example also provides an implementation of a class called MailslotPermissionEntry. The PermissionAccessType property is then privately set to the type of MailslotPermissionEntry. The TagNames property is set privately to "Name" and "Machine".

[Visual Basic] 
Imports System
Imports System.Security.Permissions
Imports System.Collections

<Serializable()> Public Class MailslotPermission
    Inherits ResourcePermissionBase

    Private innerCollection As ArrayList


    Public Sub New()
        SetNames()
    End Sub 'New


    Public Sub New(ByVal state As PermissionState)
        MyBase.New(state)
        SetNames()
    End Sub 'New


    Public Sub New(ByVal permissionAccess As MailslotPermissionAccess, ByVal name As String, ByVal machineName1 As String)
        SetNames()
        Me.AddPermissionAccess(New MailslotPermissionEntry(permissionAccess, name, machineName1))
    End Sub 'New


    Public Sub New(ByVal permissionAccessEntries() As MailslotPermissionEntry)
        SetNames()
        If permissionAccessEntries Is Nothing Then
            Throw New ArgumentNullException("permissionAccessEntries")
        End If
        Dim index As Integer

        While index < permissionAccessEntries.Length
            Me.AddPermissionAccess(permissionAccessEntries(index))
        End While
    End Sub 'New 

    Public ReadOnly Property PermissionEntries() As ArrayList
        Get
            If Me.innerCollection Is Nothing Then
                Me.innerCollection = New ArrayList()
            End If
            Me.innerCollection.InsertRange(0, MyBase.GetPermissionEntries())

            Return Me.innerCollection
        End Get
    End Property


    Friend Overloads Sub AddPermissionAccess(ByVal entry As MailslotPermissionEntry)
        MyBase.AddPermissionAccess(entry.GetBaseEntry())
    End Sub 'AddPermissionAccess


    Friend Shadows Sub Clear()
        MyBase.Clear()
    End Sub 'Clear


    Friend Overloads Sub RemovePermissionAccess(ByVal entry As MailslotPermissionEntry)
        MyBase.RemovePermissionAccess(entry.GetBaseEntry())
    End Sub 'RemovePermissionAccess


    Private Sub SetNames()
        Me.PermissionAccessType = GetType(MailslotPermissionAccess)
        Me.TagNames = New String() {"Name", "Machine"}
    End Sub 'SetNames
End Class 'MailslotPermission

<Flags()> Public Enum MailslotPermissionAccess
    None = 0
    Send = 2
    Receive = 4 Or Send
End Enum 'MailslotPermissionAccess

<Serializable()> Public Class MailslotPermissionEntry
    Private nameVar As String
    Private machineNameVar As String
    Private permissionAccessVar As MailslotPermissionAccess


    Public Sub New(ByVal permissionAccess As MailslotPermissionAccess, ByVal name As String, ByVal machineName1 As String)
        Me.permissionAccessVar = permissionAccess
        Me.nameVar = name
        Me.machineNameVar = machineName1
    End Sub 'New


    Friend Sub New(ByVal baseEntry As ResourcePermissionBaseEntry)
        Me.permissionAccessVar = CType(baseEntry.PermissionAccess, MailslotPermissionAccess)
        Me.nameVar = baseEntry.PermissionAccessPath(0)
        Me.machineNameVar = baseEntry.PermissionAccessPath(1)
    End Sub 'New


    Public ReadOnly Property Name() As String
        Get
            Return Me.nameVar
        End Get
    End Property


    Public ReadOnly Property MachineName() As String
        Get
            Return Me.machineNameVar
        End Get
    End Property


    Public ReadOnly Property PermissionAccess() As MailslotPermissionAccess
        Get
            Return Me.permissionAccessVar
        End Get
    End Property


    Friend Function GetBaseEntry() As ResourcePermissionBaseEntry
        Dim baseEntry As New ResourcePermissionBaseEntry(CInt(Me.PermissionAccess), New String() {Me.Name, Me.MachineName})
        Return baseEntry
    End Function 'GetBaseEntry


End Class 'MailslotPermissionEntry

[C#] 
using System;        
using System.Security.Permissions;  
using System.Collections;

[Serializable()]
public class MailslotPermission: ResourcePermissionBase
{
 
    private ArrayList innerCollection;

    public MailslotPermission() 
    {
        SetNames();
    }                                                                

    public MailslotPermission(PermissionState state):base(state)
    {
        SetNames();
    }  

    public MailslotPermission(MailslotPermissionAccess permissionAccess, string name, string machineName)
    {
        SetNames();
        this.AddPermissionAccess(new MailslotPermissionEntry(permissionAccess, name, machineName));              
    }

    public MailslotPermission(MailslotPermissionEntry[] permissionAccessEntries) 
    {
        SetNames();
        if (permissionAccessEntries == null)
            throw new ArgumentNullException("permissionAccessEntries");
      
        for (int index = 0; index < permissionAccessEntries.Length; ++index)
            this.AddPermissionAccess(permissionAccessEntries[index]);                          
    }

    public ArrayList PermissionEntries 
    {
        get 
        {
            if (this.innerCollection == null)                     
                this.innerCollection = new ArrayList();
            this.innerCollection.InsertRange(0,base.GetPermissionEntries()); 

            return this.innerCollection;                                                               
        }
    }

    internal void AddPermissionAccess(MailslotPermissionEntry entry) 
    {
        base.AddPermissionAccess(entry.GetBaseEntry());
    }

    internal new void Clear() 
    {
        base.Clear();
    }

    internal void RemovePermissionAccess(MailslotPermissionEntry entry) 
    {
        base.RemovePermissionAccess(entry.GetBaseEntry());
    }

    private void SetNames() 
    {
        this.PermissionAccessType = typeof(MailslotPermissionAccess);
        this.TagNames = new string[]{"Name","Machine"};
    }                                
}

[Flags]         
public enum MailslotPermissionAccess 
{
    None = 0,
    Send = 1 << 1,
    Receive = 1 << 2 | Send,
} 

[Serializable()]     
public class MailslotPermissionEntry 
{
    private string name;
    private string machineName;
    private MailslotPermissionAccess permissionAccess;

    public MailslotPermissionEntry(MailslotPermissionAccess permissionAccess, string name, string machineName) 
    {
        this.permissionAccess = permissionAccess;
        this.name = name;
        this.machineName = machineName;
    }  

    internal MailslotPermissionEntry(ResourcePermissionBaseEntry baseEntry) 
    {
        this.permissionAccess = (MailslotPermissionAccess)baseEntry.PermissionAccess;
        this.name = baseEntry.PermissionAccessPath[0]; 
        this.machineName = baseEntry.PermissionAccessPath[1]; 
    }

    public string Name 
    {
        get 
        {                
            return this.name;                
        }                        
    }

    public string MachineName 
    {
        get 
        {                
            return this.machineName;                
        }                        
    }

    public MailslotPermissionAccess PermissionAccess 
    {
        get 
        {
            return this.permissionAccess;
        }                        
    }      

    internal ResourcePermissionBaseEntry GetBaseEntry() 
    {
        ResourcePermissionBaseEntry baseEntry = new ResourcePermissionBaseEntry((int)this.PermissionAccess, new string[] {this.Name,this.MachineName});            
        return baseEntry;
    }

}

[C++] 
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Security::Permissions;
using namespace System::Collections;

[Flags]
public __value enum MailslotPermissionAccess {
    None = 0,
    Send = 1 << 1,
    Receive = 1 << 2 | Send,
};

[Serializable]
public __gc class MailslotPermissionEntry {
private:
    String* name;
    String* machineName;
    MailslotPermissionAccess permissionAccess;

public private:
    MailslotPermissionEntry(ResourcePermissionBaseEntry* baseEntry) {
        this->permissionAccess = (MailslotPermissionAccess)baseEntry->PermissionAccess;
        this->name = baseEntry->PermissionAccessPath->Item[0]->ToString();
        this->machineName = baseEntry->PermissionAccessPath->Item[1]->ToString();
    }

    ResourcePermissionBaseEntry* GetBaseEntry() {
        String* newStrings[] = {this->Name, this->MachineName};
        ResourcePermissionBaseEntry* baseEntry = 
            new ResourcePermissionBaseEntry((int)(this->PermissionAccess), newStrings);
        return baseEntry;
    }

public:
    MailslotPermissionEntry(MailslotPermissionAccess permissionAccess, String* name, String* machineName) {
        this->permissionAccess = permissionAccess;
        this->name = name;
        this->machineName = machineName;
    }

    __property String* get_Name() {
        return this->name;
    }

    __property String* get_MachineName() {
        return this->machineName;
    }

    __property MailslotPermissionAccess get_PermissionAccess() {
        return this->permissionAccess;
    }
};

[Serializable]
public __gc class MailslotPermission : public ResourcePermissionBase {
private:
    ArrayList* innerCollection;

    void SetNames() {
        this->PermissionAccessType = __typeof(MailslotPermissionAccess);
        String* newStrings[] = {S"Name", S"Machine"};
        this->TagNames = newStrings;
    }

public private:
    void AddPermissionAccess(MailslotPermissionEntry* entry) {
        ResourcePermissionBase::AddPermissionAccess(entry->GetBaseEntry());
    }

    void Clear() {
        ResourcePermissionBase::Clear();
    }

    void RemovePermissionAccess(MailslotPermissionEntry* entry) {
        ResourcePermissionBase::RemovePermissionAccess(entry->GetBaseEntry());
    }

public:
    MailslotPermission() {
        SetNames();
    }

    MailslotPermission(PermissionState state) : ResourcePermissionBase(state) {
        SetNames();
    }

    MailslotPermission(MailslotPermissionAccess permissionAccess, String* name, String* machineName) {
        SetNames();
        this->AddPermissionAccess(new MailslotPermissionEntry(permissionAccess, name, machineName));
    }

    MailslotPermission(MailslotPermissionEntry* permissionAccessEntries[]) {
        SetNames();
        if (permissionAccessEntries == 0)
            throw new ArgumentNullException(S"permissionAccessEntries");

        for (int index = 0; index < permissionAccessEntries->Length; ++index)
            this->AddPermissionAccess(permissionAccessEntries[index]);
    }

    __property ArrayList* get_PermissionEntries() {
        if (this->innerCollection == 0)
            this->innerCollection = new ArrayList();
        this->innerCollection->InsertRange(0, __try_cast<ICollection*>(ResourcePermissionBase::GetPermissionEntries()));

        return this->innerCollection;
    }
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Security.Permissions

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

ResourcePermissionBase Members | System.Security.Permissions Namespace

Show:
© 2014 Microsoft