2

I am confused about is there any way to access static class property values using the static class name defined inside a string variable Example :

I have a static class like below

public static class CoreConstants
{
    public const string  HostAddress= "someaddress";
}

And I have a string variable like

private string staticClassName="CoreConstants";

So is there any way to get the value of the HostAddress field using the string?

I know we can use Activator.CreateInstance() method if the class is a normal class , and using the instance we can get the values. But what about if the class is a static class ?

My real situation is like I have few static classes which holds constants for different language. Each request will pass a language indicator string, so using the string I need to get the exact message from the particular static class .

7
  • 1
    Why don't you just resource files and the globalization support ASP.NET provides? While it is pretty easy to accomplish what you want using reflection, it doesn't sound like the best approach at all Commented Jul 1, 2018 at 12:02
  • Agreed - why not use a resource if you're localizing strings? Add > New Item > Resources File. Commented Jul 1, 2018 at 12:03
  • @CamiloTerevinto Good option ,but if we are using globalization then the language conversion is automatic ? or can we define/hardcoded the language labels in the resource files? Commented Jul 1, 2018 at 12:07
  • You can automatically select resources based on language with .NET. You could also build your own string provider. It sounds like the original approach was going to be difficult - or maybe we're/I am misunderstanding what you're trying to do. I have to localize a number of Xamarin apps, so resources have been the way to go. learn.microsoft.com/en-us/dotnet/standard/… Commented Jul 1, 2018 at 12:10
  • It all happens automatically if you use what ASP.NET provides. See here for some basics (nevermind it says ASP.NET MVC, the same principles apply) Commented Jul 1, 2018 at 12:10

1 Answer 1

4

You first have to get the type the property belongs to:

var type = Type.GetType("CoreConstants");

Be aware that you need a fully qualified name, including the namespace and assembly the type is defined in. Otherwise the type-loader will just look in mscorlib making GetType return null.

If you have the type, simply call Type.GetProperty or Type.GetField depending on if it´s a field or a property you want to access:

var field = type?.GetField("HostAddress");

Finally get the value of the static field:

var value = field?.GetValue(null);

As your field is static, the paramater provided to GetValue is null. If it were an instance-field, you´d have to provide the instance.

Sign up to request clarification or add additional context in comments.

6 Comments

@ArunprasanthKV What do you mean exactly? You can do this from anywhere, you can even get types in a third-party assembly.
Cool its working , Not related to the current scenario but still I have a doubt I have tried to execute Activator.CreateInstance("string","string") and it shows error when i do the same from class library
Why would you want to create a string by Activator? Anyway string is declared in mscorlib, so the first parameter would be the fuly qualified name of that assembly. Or omit that completely, as Activator also searches in that assembly by default - no need to provide that again.
not exactly , string is the name of a class , i am trying to create an instance of a class using its name saved in string
But by providing two string-parameters you´re referencing this overload, whose first arg is the assemblies name.
|

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.