Skip to content

Commit 5e8f2c8

Browse files
committed
Remove dependency on System.Security.Permissions package for .NET Standard and .NET Core
System.Security.Permissions package is used to check that ReflectionPermission is granted to perform a small optimization for DynamicMethod, however in .NET Standard and .NET Core permissions are not really supported and SecurityManager.IsGranted always returns false. Also for .NET Framework the obsoleted SecurityManager.IsGranted was replaced with a preferred method of checking permissions.
1 parent 2a6021f commit 5e8f2c8

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

src/NHibernate/Bytecode/Lightweight/ReflectionOptimizer.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,22 @@ protected virtual void ThrowExceptionForNoDefaultCtor(System.Type type)
101101

102102
protected DynamicMethod CreateDynamicMethod(System.Type returnType, System.Type[] argumentTypes)
103103
{
104-
System.Type owner = mappedType.IsInterface ? typeof (object) : mappedType;
105-
#pragma warning disable 612,618
106-
bool canSkipChecks = SecurityManager.IsGranted(new ReflectionPermission(ReflectionPermissionFlag.MemberAccess));
107-
#pragma warning restore 612,618
104+
var owner = mappedType.IsInterface ? typeof (object) : mappedType;
105+
var canSkipChecks = CanSkipVisibilityChecks();
108106
return new DynamicMethod(string.Empty, returnType, argumentTypes, owner, canSkipChecks);
109107
}
110108

109+
private static bool CanSkipVisibilityChecks()
110+
{
111+
#if NETFX
112+
var permissionSet = new PermissionSet(PermissionState.None);
113+
permissionSet.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.MemberAccess));
114+
return permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet);
115+
#else
116+
return false;
117+
#endif
118+
}
119+
111120
private static void EmitCastToReference(ILGenerator il, System.Type type)
112121
{
113122
if (type.IsValueType)

src/NHibernate/Driver/OdbcDriver.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Data;
44
using System.Data.Common;
5-
using System.Data.Odbc;
65
using NHibernate.SqlTypes;
76
using NHibernate.Util;
87
using Environment = NHibernate.Cfg.Environment;
@@ -47,12 +46,12 @@ public OdbcDriver()
4746
#else
4847
public override DbConnection CreateConnection()
4948
{
50-
return new OdbcConnection();
49+
return new System.Data.Odbc.OdbcConnection();
5150
}
5251

5352
public override DbCommand CreateCommand()
5453
{
55-
return new OdbcCommand();
54+
return new System.Data.Odbc.OdbcCommand();
5655
}
5756
#endif
5857

src/NHibernate/Driver/OleDbDriver.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Data.Common;
3-
using System.Data.OleDb;
43

54
namespace NHibernate.Driver
65
{
@@ -26,12 +25,12 @@ public OleDbDriver()
2625
#else
2726
public override DbConnection CreateConnection()
2827
{
29-
return new OleDbConnection();
28+
return new System.Data.OleDb.OleDbConnection();
3029
}
3130

3231
public override DbCommand CreateCommand()
3332
{
34-
return new OleDbCommand();
33+
return new System.Data.OleDb.OleDbCommand();
3534
}
3635
#endif
3736

src/NHibernate/NHibernate.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,10 @@
5353

5454
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.0'">
5555
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.4.1" />
56-
<PackageReference Include="System.Security.Permissions" Version="4.4.1" />
5756
</ItemGroup>
5857

5958
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
6059
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.4.1" />
61-
<PackageReference Include="System.Security.Permissions" Version="4.4.1" />
6260
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
6361
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.3.0" />
6462
</ItemGroup>

0 commit comments

Comments
 (0)