-1

I want to modify the below code to be able to use a private method

        //use reflection to Load the data
    var method =
                    typeof(MemberDataFactory)
                    .GetMethod("LoadData")
                    .MakeGenericMethod(new [] { data.GetType() })
                    .Invoke(this, null);

Ive tried the following with no luck:

        //use reflection to Load the data
    var method =
                    typeof(MemberDataFactory)
                    .GetMethod("LoadData")
                    .MakeGenericMethod(new [] { data.GetType() })
                    .Invoke(this, BindingFlags.Instance | BindingFlags.NonPublic, null , null, null);

Also what is "var" in terms of this code? Id prefer to specify its type instead of using var.

Thanks!

MichaelTaylor3D
  • 1,615
  • 3
  • 18
  • 32
  • 1
    What do you mean by "no luck"? Have you got an exception? Or code did not compile? – Sergei Rogovtcev Sep 01 '12 at 17:49
  • 1
    `var` is implicit typing, but it is completely static, not dynamic. See http://msdn.microsoft.com/en-us/library/bb383973.aspx (contrast dynamic http://msdn.microsoft.com/en-us/library/dd264741.aspx) – Tim S. Sep 01 '12 at 17:51
  • Have you tried reading [this](http://msdn.microsoft.com/en-us/library/bb383973.aspx)? – Sergei Rogovtcev Sep 01 '12 at 17:51
  • 4
    The flags should be used for `GetMethod`, not `Invoke` – Kevin Gosse Sep 01 '12 at 17:51
  • Null reference exception, the first code I posted works fine on public methods. The second gives a null reference exception when used on a private method – MichaelTaylor3D Sep 01 '12 at 17:51
  • @KooKiz that was the problem, could you post it as an answer so I can mark it correct? – MichaelTaylor3D Sep 01 '12 at 17:55
  • @TimS. sorry I got the terminology wrong, but I meant that instead of var I want to specify its type. – MichaelTaylor3D Sep 01 '12 at 17:55
  • Im not a 100% sure why someone just down voted me, I see this as a legitimate question. – MichaelTaylor3D Sep 01 '12 at 17:59
  • @Serg Rogovtsev I know what var means, I meant what is its type as it refers to the code, it could be a string, an int or something else. I always like to specify its type instead of using var. – MichaelTaylor3D Sep 01 '12 at 18:06
  • @MichaelTaylor3D then go on and specify it, what stops you? – Sergei Rogovtcev Sep 01 '12 at 20:05
  • @SergRogovtsev I wasnt sure what type the reflection code returns. A very similiar code returns of type MemberInfo, not this one. I was hoping someone who was knowledgeable in reflection know what type that code returns. – MichaelTaylor3D Sep 02 '12 at 00:11
  • @MichaelTaylor3D MSDN very clearly [states](http://msdn.microsoft.com/en-us/library/4k9x6bc0%28v=vs.80%29.aspx) that `Invoke` returns `object`. Besides you can simply hold your mouse over `var` in Visual Studio and see type in tooltip. (And no, no similar code could return `MethodInfo`.) – Sergei Rogovtcev Sep 02 '12 at 07:46
  • @SergRogovtsev putting aside several false assumptions your making, Thank you for your help! – MichaelTaylor3D Sep 02 '12 at 14:34
  • @MichaelTaylor3D which "false assumptions" do you mean? – Sergei Rogovtcev Sep 02 '12 at 15:32
  • @SergRogovtsev It doesn't matter, its all beyond the scope of this question. But your information was vital in helping me solve my dilemma. Thanks! – MichaelTaylor3D Sep 02 '12 at 19:10

1 Answers1

3

You want to use this overload of Type.GetMethod(), which is where you pass your binding flags. The default .GetMethod(string) only looks for public methods, so it returns null, hence your null reference exception.

Your code should be something more like:

var method =
        typeof(MemberDataFactory)
        .GetMethod("LoadData", BindingFlags.Instance | BindingFlags.NonPublic) // binding flags go here
        ...
CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138