1

I am trying to get the constant value using the siting value, but I am unable to achieve that can you help me to do so

using System;
public static class Constants
{
    public static class HostServer
    {
        public static string ABC = "abc.com";
    }
    public static class WMS
    {
        public const string URL = "/create/user";
    }
}
public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        var x = Constants.GetType().GetProperty("ABC").GetValue(Constants, null).ToString();
        Console.WriteLine(x);
    }
}

Thanks in advance

  • try `var x = typeof(Constants).GetProperty("...` – Sergey Kalinichenko Sep 29 '21 at 17:49
  • 2
    The question is though, why use reflection? – DavidG Sep 29 '21 at 17:59
  • `var x = typeof(Constants).GetProperty("ABC").GetValue(Constants,null).ToString();` i got this below error `'Constants' is a 'type' but is used like a 'variable'` – S Deepakkumar Sep 29 '21 at 17:59
  • @DavidG can you suggest a sample for the above with an example – S Deepakkumar Sep 29 '21 at 18:05
  • @SDeepakkumar you have that property in another class, it's not in `Constants`, rather it's in `HostServer`... – Trevor Sep 29 '21 at 18:05
  • 1
    I mean, why can't you just use `var x = Constants.HostServer.ABC;`? – DavidG Sep 29 '21 at 18:07
  • @zaggler is there ant sample code to achieve this – S Deepakkumar Sep 29 '21 at 18:07
  • But if you REALLY need to use reflection to get ABC then try this: `var x = typeof(Constants.HostServer).GetField("ABC", BindingFlags.Public | BindingFlags.Static).GetValue(null);` – DavidG Sep 29 '21 at 18:09
  • @DavidG we can use what you have mentioned, but I will send a string parameter to a method to access the constant property – S Deepakkumar Sep 29 '21 at 18:09
  • It looks to me like you're building up configuration settings. As mentioned elsewhere, reflection is probably not needed for this. Look into the `ConfigurationManager` class. see: https://learn.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager?view=net-5.0 – Jonathan Sep 29 '21 at 18:16
  • https://xyproblem.info – Ian Kemp Sep 29 '21 at 18:24
  • Does this answer your question? [How to get fields and their values from a static class in referenced assembly](https://stackoverflow.com/questions/7334067/how-to-get-fields-and-their-values-from-a-static-class-in-referenced-assembly) – Trevor Sep 29 '21 at 18:24

1 Answers1

4

First of all, you need to use GetField, not GetProperty. Secondly, you should specify the binding flags since constants are essentially static. And finally, you need to use the full type name of the class since you have nested classes. With all of that, this will get you the constant value:

var x = typeof(Constants.HostServer) // <-- Use full class name here
   .GetField("ABC", BindingFlags.Public | BindingFlags.Static) // <-- binding flags
   .GetValue(null); // <-- static fields don't need an instance object
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • Work's fine thank you – S Deepakkumar Sep 29 '21 at 18:17
  • @SDeepakkumar I would encourage you do some null checking, `GetField` can return `null`... So something like: `var x = typeof(Constants.HostServer) // <-- Use full class name here .GetField("ABC", BindingFlags.Public | BindingFlags.Static) // <-- binding flags ?.GetValue(null);` – Trevor Sep 29 '21 at 18:18