Dapper.Contrib icon indicating copy to clipboard operation
Dapper.Contrib copied to clipboard

Access to static readonly ConcurrentDictionary in SqlMapperExtensions

Open kiquenet opened this issue 4 years ago • 2 comments

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;

kiquenet avatar Aug 07 '20 10:08 kiquenet

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?

NickCraver avatar Aug 19 '20 01:08 NickCraver

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

dogac00 avatar Aug 19 '20 23:08 dogac00