DapperLib / Dapper.Contrib

Dapper community contributions - additional extensions for Dapper
Other
261 stars 94 forks source link

Access to static readonly ConcurrentDictionary in SqlMapperExtensions #88

Open kiquenet opened 4 years ago

kiquenet commented 4 years ago

How access to TypeTableName in Dapper.Contrib.Extensions.SqlMapperExtensions class using Reflection?

private static readonly ConcurrentDictionary<RuntimeTypeHandle, string> TypeTableName = new ConcurrentDictionary<RuntimeTypeHandle, string>();

Not cannot set null to SqlMapperExtensions.TableNameMapper because use TypeTableName

https://github.com/StackExchange/Dapper/blob/main/Dapper.Contrib/SqlMapperExtensions.cs#L280

   ```
 Delegate[] delegados  = Dapper.Contrib.Extensions.SqlMapperExtensions.TableNameMapper.GetInvocationList();
        foreach (TableNameMapperDelegate delegateTableNameMapper in delegados)
            Dapper.Contrib.Extensions.SqlMapperExtensions.TableNameMapper -= delegateTableNameMapper; 

        Delegate.RemoveAll(Dapper.Contrib.Extensions.SqlMapperExtensions.TableNameMapper, Dapper.Contrib.Extensions.SqlMapperExtensions.TableNameMapper);

        Dapper.Contrib.Extensions.SqlMapperExtensions.TableNameMapper -= tableNameDelegate;
        Dapper.Contrib.Extensions.SqlMapperExtensions.TableNameMapper = null;
NickCraver commented 4 years ago

Can we back up slightly to what you're trying to do? Generally speaking if you need to reflect Dapper and it's a valid use case, we have a gap in the API which we could potentially solve in other ways!

What are you trying to read or change?

dogac00 commented 4 years ago

I think this should work, but haven't tested it.

var typeTableName = typeof(SqlMapperExtensions)
                .GetFields(BindingFlags.Static | BindingFlags.NonPublic)
                .FirstOrDefault(field => field.Name == "TypeTableName")
                .GetValue(null);