4

I tried code below in .NET 3.5 but mi is null. How to call private generic method so that type parameter can be passed at runtime? If SaveEntityGeneric is marked as public this code works OK but I dont wat to make it public since it is only used in other method in same class to pass this class type using GetType().

using System.Reflection;

public class Main1
{
    static void Main()
    {
        new Class1().Test();
    }
}

class Class1
{

    public void Test()
    {
        var mi = GetType().GetMethod("SaveEntityGeneric", BindingFlags.NonPublic);
        // why mi is null ?
        var gm = mi.MakeGenericMethod(GetType());
        gm.Invoke(this, null);
    }

    void SaveEntityGeneric<TEntity>()
    {
    }
}
Andrus
  • 26,339
  • 60
  • 204
  • 378
  • 1
    This question has everything you'd ever need : [Select Right Generic Method with Reflection](http://stackoverflow.com/questions/3631547/select-right-generic-method-with-reflection) – spender Sep 13 '11 at 15:59
  • 1
    @spender: That question is about public static methods (which don't require binding flags to find). This one is about private instance methods. It's not a duplicate. – StriplingWarrior Sep 13 '11 at 16:01
  • 1
    @StriplingWarrior: agreed. Luckily it takes 5 people to close a question. – spender Sep 13 '11 at 16:04

2 Answers2

6

The binding flags are tricky to get right on this. Use BindingFlags.NonPublic | BindingFlags.Instance.

var mi = GetType().GetMethod("SaveEntityGeneric", 
                             BindingFlags.NonPublic | BindingFlags.Instance);
var gm = mi.MakeGenericMethod(GetType());
gm.Invoke(this, null);
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • Thank you. It Test() method is called from ASP .NET MVC 2 controller (from other assembly) GetMethod() still returns null. How to make it work if called from controller also ? – Andrus Sep 13 '11 at 16:54
  • 1
    @Andrus: NonPublic reflection requires you to be running in Full Trust mode, which ASP.NET doesn't do by default (and which may not be a good idea to do generally). If you understand and accept the risks, set up your application to run with Full Trust. But you may want to consider changing your approach to avoid the need for accessing private methods. – StriplingWarrior Sep 13 '11 at 17:13
  • I dont understand which is the risk if object calls its own private method. Without reflection private method call works OK . It looks like bug in .NET. GetMethod should return object own private methods always. Only way seems to make those methods public which is ugly. – Andrus Sep 13 '11 at 17:59
  • I tried to call public static method from MVC controller using same code. GetMethod does not find it in MVC but finds in console application. So the public static method call is also not supported in ASP .NET ? – Andrus Sep 13 '11 at 18:32
  • @Andrus: Your method is now public, but your class is still private. It should work if you make your class public as well. – StriplingWarrior Sep 13 '11 at 18:57
  • No, everything is public. I posted this as separate question in http://stackoverflow.com/questions/7406891/how-to-call-static-method-from-asp-net-mvc-controller-in-c with new code sample – Andrus Sep 14 '11 at 06:28
0

Simply make it internal, that will prevent from usage that method outside the assembly

Alan Turing
  • 2,482
  • 17
  • 20
  • internal method appears in intellisense if called in this assembly for EntityBase object. I'm looking for a way to make it private and call public method from MVC2 controller which calls this private method. – Andrus Sep 14 '11 at 12:48