0

I created a method to return a default field value from any class. I am trying to use Reflection to get the value, but it doesn't work.

Here is the class with the default value that I want (StoredProcedure):

namespace Services.Data.Report
{
  public class PayrollReport
  {
    public string FullName { get; set; }
    public DateTime WeekStart { get; set; }
    public decimal PerDiem { get; set; }
    public decimal StPay { get; set; }
    public decimal OtPay { get; set; }
    public decimal StraightHours { get; set; }
    public decimal OverTimeHours { get; set; }

    [DefaultValue("report_payrollSummary")]
    public string StoredProcedure { get; set; }
  }
}

I have this method that will allow me to pass the name of the class, and hopefully get the desired field value:

namespace Services
{
  public class DynamicReportService : IDynamicReportService
  {     
    public string GetDynamicReport(string className)
    {
        System.Reflection.Assembly assem = typeof(DynamicReportService).Assembly;
        var t = assem.GetType(className);
        var storedProcedure = t?.GetField("StoredProcedure").ToString();
        return storedProcedure;
    }
  }
}

I have also tried this, but get the same results:

var t = Type.GetType(className);

The problem is that t is never set.

I am trying to call it with something like this:

var storedProc = _dynamicReportService.GetDynamicReport("Services.Data.Report.PayrollReport");

Is there another way to pass the Class by name and be able to access the fields, methods, and other properties?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
davids
  • 5,397
  • 12
  • 57
  • 94
  • 2
    Are you passing the full assembly qualified name of the class in className? it should be "namespace.classname, assemblyname" so "Services.Data.Report.PayrollReport, Services.Data.Report" assuming the namespace matches the assembly. To check you could run this in the class and capture the output: this.GetType().AssemblyQualifiedName – jimpaine Oct 22 '16 at 19:06
  • What are you expecting to get back when calling `GetDynamicReport`? In your example, are you expecting to get string with value "report_payrollSummary"? – RVid Oct 22 '16 at 19:08
  • If you want to get the value of an `Attribute` on a `Property` you will need to get the property (it's not a field) and use reflection on that to get the attribute. `ToString()` isn't going to give you that. See for example http://stackoverflow.com/q/6637679/224370 – Ian Mercer Oct 22 '16 at 19:19
  • 1
    That's not a field, it's a property. Call `GetProperty("StoredProcedure")`. Look at @SomeUser 's answer – 15ee8f99-57ff-4f92-890c-b56153 Oct 22 '16 at 19:24

1 Answers1

2

Try this:

System.Reflection.Assembly assembly = typeof(DynamicReportService).Assembly;
var type = assembly.GetType(className);
var storedProcedurePropertyInfo = type.GetProperty("StoredProcedure"); 
var defaultValueAttribute = storedProcedurePropertyInfo.GetCustomAttribute<DefaultValueA‌​ttribute>();
return defaultValueAttribute.Value.ToString();

First we will get the StoredProcedure PropertyInfo from the type, then we will look for the property DeafultValueAttribute using GetCustomAttribute<T> extension and in the end we will take the attribute value and return it.

davids
  • 5,397
  • 12
  • 57
  • 94
YuvShap
  • 3,825
  • 2
  • 10
  • 24
  • 1
    It looks better to use `var deafultValueAttribute = storedProcedurePropertyInfo.GetCustomAttribute();` (extension method since .NET 4.5 (2012), requires `using System.Reflection;`). – Jeppe Stig Nielsen Oct 22 '16 at 22:48