Why DapperRow.GetType() return null?

Error processing SSI file

Answers

  1. Gallo

    • 2020/5/12

    DapperRow is specifically built and utilized within Dapper to provide highly optimized row returns without reiterating header information. This is to help condense the size of the object and reduce redundant data, making it more efficient.

    However, it would appear that the StackExchange team took the meta programming even further than a first glance would indicate.

    DapperRow implements the System.Dynamic.IDynamicMetaObjectProvide interface, which requires that the GetMetaObject method be implemented:

    System.Dynamic.DynamicMetaObject System.Dynamic.IDynamicMetaObjectProvider.GetMetaObject(
        System.Linq.Expressions.Expression parameter)
    {
        return new DapperRowMetaObject(parameter, 
            System.Dynamic.BindingRestrictions.Empty, this);
    }
    

    DapperRowMetaObject is a custom implementation of DynamicMetaObject that essentially hijacks and overrides what methods can be invoked against the dynamic type and what those calls should translate to. In this case, calls to anything other than the DapperRow's IDictionary.Item getter or the DapperRow.SetValue will fail since they are always routed to those two calls, but the value will be defaulted to null for any "get" calls where the target property does not exist in the table.

    public bool TryGetValue(string name, out object value)
    {
        var index = table.IndexOfName(name);
        if (index < 0)
        { // doesn't exist
            value = null;
            return false;
        }
        ...
    }
    

    At that point, any methods invoked on a null dynamic value will throw a RuntimeBinderException:

    RuntimeBinderException: Cannot perform runtime binding on a null reference

    You can easily test this hypothesis by replacing GetType() with another call that will throw the exact same exception:

    var rec = cn.Query("select getdate() as D").Single();
    var t = rec.AsEnumerable();
    Console.WriteLine(t.ToList());
    

    Keep in mind, the underlying type information of any properties on the dynamic object itself can still be accessed directly:

    var rec = cn.Query("select getdate() as D").Single();
    var t = rec.D.GetType();
    Console.WriteLine(t.Name);
    
  2. Jamal

    • 2018/11/17

    Query() return private class DapperRow instances to be treated as dynamic objects. I found a strange thing: DapperRow's .GetType() return null.

  3. Juelz

    • 2020/6/8

    I found a strange thing: DapperRow's .GetType() return null. Here's the sample code to reproduce the problem. Create a C# project, reference Dapper and open a connection to SQL Server (or other database), use .Query() to execute simple select query and retrieve the first row of result.

  4. Alijah

    • 2018/6/14

    dapper .query() return private class dapperrow instances treated dynamic objects. found strange thing: dapperrow's .gettype() return null.

  5. Reed

    • 2017/2/9

    I found a strange thing: DapperRow's .GetType() return null. Here's the sample code to reproduce the problem. Create a C# project, reference Dapper and open a connection to SQL Server (or other database), use .Query() to execute simple select query and retrieve the first row of result.

  6. Kaiser

    • 2016/12/6

    GetClassName() => typeof(DapperRow).FullName;. string ICustomTypeDescriptor.GetComponentName() => null;. private static readonly TypeConverter s_converter 

  7. Landen

    • 2017/12/19

    Type Name Type.GetType result typeof Result Type.GetType with assembly name System.Boolean success success success ConsoleApp.Class1 success success success System.Xml.Schema.XmlSchema null success success MyClassLibrary.ClassyClass null success success To load by string name the framework classes, fully qualifiy the assembly name.

  8. Otto

    • 2017/7/25

    GetType().ToString()); }. But it always fails on the MessageBox with the error Cannot perform runtime binding on a null reference . If I use this instead:

  9. Jad

    • 2017/6/17

    Dapper - a simple object mapper for .Net. Contribute to DapperLib/Dapper development by creating an account on GitHub.

  10. Mustafa

    • 2018/8/5

    commandType = null); {; return Query<DapperRow>(cnn, sql, param as object, GetType(), new[] { typeof(TFirst), typeof(TSecond), typeof(TThird), 

  11. Gashi

    • 2019/3/18

    /// < returns >The number of rows affected.</ returns > public static int Execute ( this IDbConnection cnn , string sql , object param = null , IDbTransaction transaction = null , int ? commandTimeout = null , CommandType ?

  12. Rodrigo

    • 2017/12/24

    The mapper only "see" what's configured, if EndConfig() doesn't get called for If type t hasn't been stored by StoreType, returns null.

  13. Dario

    • 2020/1/21

    The solution is to do something like this. You need to add the return type of the query to dynamic and then cast each row into an IDictionary<string, object>. Once you do that, you'll be able to get the value for your query by key like so: IEnumerable<dynamic> query = db.Query<dynamic> ("SELECT SUM (UserRating) as 'Sum', AVG (UserRating) as

  14. Brentley

    • 2015/7/14

    Hi, when I call the property "IsDefault" on an object inheriting from SettingsBase, I get an error because "this.GetType() in line 5 below 

Comments are closed.

More Posts