2

GetMethod does not find public static method if called from ASP .NET MVC controller. (From console application it work OK).

To solve this dummy SaveEntityGenericWrapper method is used.

How to remove SaveEntityGenericWrapper from code ?

Why GetMethod("SaveEntityGeneric") returns null but GetMethod("SaveEntityGenericWrapper") works if called from ASP .NET MVC 2 controller ?

How to make SaveEntityGeneric private if partial trust is used in MVC2 ?

public class EntityBase() {

    public void SaveEntity(EntityBase original)
    { 
        var method = GetType().GetMethod("SaveEntityGenericWrapper");
        // why this line returns null if called from ASP .NET MVC 2 controller:
        // method = GetType().GetMethod("SaveEntityGeneric");
        var gm = method.MakeGenericMethod(GetType());
        gm.Invoke(this, new object[] { original, this });
    }

    // Dummy wrapper reqired for mvc reflection call only. 
    // How to remove it?
    public List<IList> SaveEntityGenericWrapper<TEntity>(TEntity original, TEntity modified)
        where TEntity : EntityBase, new()
    {
        return SaveEntityGeneric<TEntity>(original, modified);
    }

    public static List<IList> SaveEntityGeneric<TEntity>(TEntity original, TEntity modified)
                where TEntity : EntityBase, new()
    { ... actual work is performed here  }
}

2 Answers 2

1

You need to specify BindingFlags in the GetMethod call so that static methods are retured (I think that by default only public instance methods are returned)

 var method = GetType().GetMethod("SaveEntityGenericWrapper",
                                  BindingFlags.Static|BindingFlags.Public);
Sign up to request clarification or add additional context in comments.

2 Comments

From the docs fors GetMethod(string): "The search includes public static and public instance methods."
@SWeko: I added bindingflags you recommended but problem persist. Note that code works OK in console application. Issue occurs if SaveEntity method is called from .NET 3.5 MVC2 controller.
0

Simply do not make problems with private, trying to solve bravely more complex problems.

See your previous post, how simple it was.

At least, use

internal static List<IList> SaveEntityGeneric<TEntity>(TEntity original, TEntity modified)
                where TEntity : EntityBase, new()
{
    ... 
}

2 Comments

In his example the SaveEntityGeneric method is public, at least that's what he showed in the question.
@Artur: I changed SaveEntityGeneric from public to internal and tried GetMethod on it. Problem persists. I don'nt understand your answer: if public static method is not found, how changing it to internal static enables its finding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.