internal PermissionToken GetToken(Type cls, IPermission perm)
{
BCLDebug.Assert( cls != null, "Must pass in valid type" );
IntPtr typePtr = cls.TypeHandle.Value;
object tok = m_handleTable[typePtr];
if (tok == null)
{
String typeStr = cls.AssemblyQualifiedName;
tok = m_tokenTable != null ? m_tokenTable[typeStr] : null; // Assumes asynchronous lookups are safe
if (tok == null)
{
lock (this)
{
if (m_tokenTable != null)
tok = m_tokenTable[typeStr]; // Make sure it wasn't just added
else
m_tokenTable = new Hashtable( m_size, 1.0f, new PermissionTokenKeyComparer( CultureInfo.InvariantCulture ) );
if (tok == null)
{
if (perm != null)
{
if (CodeAccessPermission.CanUnrestrictedOverride(perm))
tok = new PermissionToken( m_index++, PermissionTokenType.IUnrestricted, typeStr );
else
tok = new PermissionToken( m_index++, PermissionTokenType.Normal, typeStr );
}
else
{
if (cls.GetInterface(s_unrestrictedPermissionInferfaceName) != null)
tok = new PermissionToken( m_index++, PermissionTokenType.IUnrestricted, typeStr );
else
tok = new PermissionToken( m_index++, PermissionTokenType.Normal, typeStr );
}
m_tokenTable.Add(typeStr, tok);
m_indexTable.Add(m_index - 1, tok);
PermissionToken.s_tokenSet.SetItem( ((PermissionToken)tok).m_index, tok );
}
if (!m_handleTable.Contains( typePtr ))
m_handleTable.Add( typePtr, tok );
}
}
else
{
lock (this)
{
if (!m_handleTable.Contains( typePtr ))
m_handleTable.Add( typePtr, tok );
}
}
}
if ((((PermissionToken)tok).m_type & PermissionTokenType.DontKnow) != 0)
{
if (perm != null)
{
BCLDebug.Assert( !(perm is IBuiltInPermission), "This should not be called for built-ins" );
if (CodeAccessPermission.CanUnrestrictedOverride(perm))
((PermissionToken)tok).m_type = PermissionTokenType.IUnrestricted;
else
((PermissionToken)tok).m_type = PermissionTokenType.Normal;
((PermissionToken)tok).m_strTypeName = perm.GetType().AssemblyQualifiedName;
}
else
{
BCLDebug.Assert( cls.GetInterface( "System.Security.Permissions.IBuiltInPermission" ) == null, "This shoudl not be called for built-ins" );
if (cls.GetInterface(s_unrestrictedPermissionInferfaceName) != null)
((PermissionToken)tok).m_type = PermissionTokenType.IUnrestricted;
else
((PermissionToken)tok).m_type = PermissionTokenType.Normal;
((PermissionToken)tok).m_strTypeName = cls.AssemblyQualifiedName;
}
}
return (PermissionToken)tok;
}