locked
Calling a function with a variable filename in App_Code RRS feed

  • Question

  • User1555333800 posted

    Our departments have their own selections of reports.
    We need, based on departmentId, to call a list of functions corresponding to their report selections.

    Is there a way to call a Function in a file in the App_Code directory when the filename is a variable from a db query?
    The function has the same name "ReportData" in all reportfiles

    foreach (var filename in listoffilenames_read_from_db_query)
    {
    var result = filename .ReportData(departmentId);
    ...
    }

    Friday, February 16, 2018 1:42 PM

Answers

  • User-821857111 posted

    You can do this using Reflection. I am assuming that you have a setup along these lines:

    App_Code
        Admin.cshtml //reports for Admin
        Sales.cshtml //reports for Sales
        etc

    Lets also say you have a function named Test in your reports, and the report takes some parameters:

    @functions {
        public static string Test(DateTime startDate, DateTime endDate)
        {
            return "Hello from Admin";
            }
    }

    You can call this in the following way:

    var fileName = "Admin";
    var methodName = "Test";
    var userParams = new object[] { DateTime.Now.AddDays(-7), DateTime.Now };

    var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.FullName.StartsWith("App_Code"));
    if (assemblies.Any())
    {
        var assembly = assemblies.FirstOrDefault(a => a.DefinedTypes.Any(d => d.Name == fileName));
        var method = assembly.DefinedTypes.First(d => d.Name == fileName).DeclaredMethods.FirstOrDefault(m => m.Name == methodName);
        var message = method.Invoke(null, userParams);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 19, 2018 10:21 AM

All replies

  • User-821857111 posted

    You can do this using Reflection. I am assuming that you have a setup along these lines:

    App_Code
        Admin.cshtml //reports for Admin
        Sales.cshtml //reports for Sales
        etc

    Lets also say you have a function named Test in your reports, and the report takes some parameters:

    @functions {
        public static string Test(DateTime startDate, DateTime endDate)
        {
            return "Hello from Admin";
            }
    }

    You can call this in the following way:

    var fileName = "Admin";
    var methodName = "Test";
    var userParams = new object[] { DateTime.Now.AddDays(-7), DateTime.Now };

    var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.FullName.StartsWith("App_Code"));
    if (assemblies.Any())
    {
        var assembly = assemblies.FirstOrDefault(a => a.DefinedTypes.Any(d => d.Name == fileName));
        var method = assembly.DefinedTypes.First(d => d.Name == fileName).DeclaredMethods.FirstOrDefault(m => m.Name == methodName);
        var message = method.Invoke(null, userParams);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 19, 2018 10:21 AM
  • User1555333800 posted

    Thank You, Mike. 
    This was new to me!

    Learning something new every day!

    Wednesday, February 21, 2018 10:55 AM