Get value of static field - c#

I've got the following class:
public static class Pages
{
public static string LoggedOut = "LoggedOut.aspx";
public static string Login = "Login.aspx";
public static string Home = "Home.aspx";
}
I know I can use Pages.Home statically, but there is a reason for my question.
I wish to have a method that I can call like this:
string pageName = Pages.GetPage("Home");
etc.
C'est possible?
Thanks,
Dave

You can use the following:
var field = typeof(Pages).GetField("Home", BindingFlags.Public | BindingFlags.Static);
var value = (string)field.GetValue(null);

You can do it like Konrad suggested using reflection. But I would consider it much better design to use a dictionary and not rely on reflection for such a task.
public static class Pages
{
private static readonly IDictionary<String, String> PageMap = null;
private static Pages()
{
Pages.PageMap = new Dictionary<String, String>();
Pages.PageMap.Add("LoggedOut", "LoggedOut.aspx");
Pages.PageMap.Add("Login", "Login.aspx");
Pages.PageMap.Add("Home", "Home.aspx");
}
public static GetPage(String pageCode)
{
String page;
if (Pages.PageMap.TryGet(pageCode, out page)
{
return page;
}
else
{
throw new ArgumentException("Page code not found.");
}
}
}
You should of course adjust the error handling to your actual requirements.

Just my tuppence... if you are going to use literals ("Home"), then I would absolutely bind to the const, i.e. Pages.Home (they should probably be constants in the example given). The reflection approach might be handy if you have:
string s = ...something clever...
string page = GetPage(s);
If you do switch to const, then note that they manifest as static fields:
string s = ...something clever...
FieldInfo field = typeof(Pages).GetField(s,
BindingFlags.Static | BindingFlags.Public);
string page = (string)field.GetValue(null);
If it is used heavily you could also cache these in a dictionary.

As other have said, I'd avoid using reflection here. Also, although it adds a bit more code, an enum for the hardcoded page names is also a good idea (also suggested previously)
public enum pages { LoggedOut, Login, Home }
static Dictionary<pages, string> pageDict = new Dictionary<pages, string>() {
{pages.Home, "Home.aspx"},
{pages.Login, "Login.aspx"},
{pages.LoggedOut, "LoggedOut.aspx"}
};
public static string getPage(pages pageName)
{
return pageDict[pageName];
}

Related

Replace (and backup) Method dynamically in C#

I need to change a specific property dynamically and have been using this snipplet:
var _oldMethod = typeof(TypeName).GetProperty("OldProperty", BindingFlags.Public | BindingFlags.Static).GetMethod;
var _newMethod = typeof(OwnTypeName).GetProperty("NewProperty", BindingFlags.Public | BindingFlags.Static).GetMethod;
ReplaceMethod(_oldMethod, _newMethod);
...
private static unsafe void ReplaceMethod(MethodInfo _oldMethod, MethodInfo _newMethod)
{
var _oldMethodAddress = new IntPtr((int*)_oldMethod.MethodHandle.Value.ToPointer() + 2);
var _destination = (uint*)_oldMethodAddress.ToPointer();
*destination = (uint)_newMethod.MethodHandle.GetFunctionPointer().ToInt32();
}
Unfortunately this required some decompiling with recreating the original property. What I am looking for now is a a possibility to duplicate and kind of backup the original method and dynamically replace the old method with the new one or restore the original one.
Has anyone an idea how to implement this?
Edit:
I may should have clarified my situation further:
public static class ClassName
{
public static bool Property
{
get
{
// Conditions
}
}
}
I can't access ClassName and need to force Property to false in specific situations and need to replace it to the original return value in other situations. I have been using the ReplaceMethod above mentioned but don't want to decompile and rebuild Property from scratch (instead some kind of backup of the original Property)
First of all, you are handling properties instead of methods.
One easy approach to do this is to replace your properties type by Func<> and this will keep what you're wanting to do easier.
private static void Main(string[] args)
{
var a = new A();
a.Property = Method1;
Console.WriteLine(a.Property.Invoke());
a.Property = Method2;
Console.WriteLine(a.Property.Invoke());
Func<string> oldMethod = a.Property;
Console.WriteLine(oldMethod.Invoke());
Console.ReadLine();
}
public class A
{
public Func<string> Property { get; set; }
}
private static string Method1()
{
return "Method1";
}
private static string Method2()
{
return "Method2";
}
You can change the method as many times as you want and also keep the old one in one variable.

FieldInfo.SetValue on a Nested Static Class using Reflection

I am struggling with obtaining the "object" to be able to set a value on a nested class, and I suspect that I am going to struggle with how to manipulate it after it is set. Example follows:
public class RegistryData {
public RegistryKey rk;
}
public static class RegistryKeys {
public static class Username {
public static RegistryData Data = null;
public static string DefaultValue = "MyUsername";
}
public static class Password {
public static RegistryData Data = null;
public static string DefaultValue = "MyPassword";
}
}
The following code uses reflection to obtain the Data field, however I can not see how to obtain the "object" to pass into FieldInfo.SetValue().
static void DoReflection()
{
Type type = typeof(RegistryKeys);
Type[] nestedTypeArray = type.GetNestedTypes(BindingFlags.Public | BindingFlags.Static);
foreach(Type t in nestedTypeArray)
{
FieldInfo field = t.GetField("Data"); // Obtain the Data field
// Issue 1: how to allocate the Data Field within the nested class
field.SetValue( ??? object ??? , new RegistryData()); <---
// Issue 2: how to access the new class within the nested class
field.GetValue( ??? what ??? ).rk = Registry.LocalMachine;
}
}
Thanks.
OK, turns out that with a little more research (asking the question slightly differently in Google) and I have found the answer:
field.SetValue( null , new RegistryData());
((RegistryData)(field.GetValue(null))).rk = Registry.LocalMachine;
According to the answer here: Is it possible to set this static private member of a static class with reflection?
It states in the code section:
// Normally the first argument to "SetValue" is the instance
// of the type but since we are mutating a static field we pass "null"
I am not going to suggest that I understand that. But the code is working! Rather than just delete this question I will post the answer here for others as I did struggle to find answer.

Following the DRY principle in ASP.NET

I have just recently got involved in a classic ASP.NET project which contains lots of storing and reading values from the session and query strings. This could look something like the following:
Session["someKey"]=someValue;
And somewhere else in the code the value in the session is read. Clearly this violates the DRY principle since you'll have the literal string key spread out all over the code. One way to avoid this could be to store all keys as constants that could be referenced everywhere there is a need to read and write to the session. But I'm not sure that's the best way to do it. How would you recommend I best handle this so that I don't violate the DRY principle?
Create a separate public class where you can define your constants, e.g
public class SessionVars
{
public const string SOME_KEY = "someKey";
public const string SOME_OTHER_KEY = "someOtherKey";
}
and then anywhere in your code you can access session variables like this:
Session[SessionVars.SOME_KEY]=someValue;
This way you can get IntelliSence and other bells and whistles.
I think you're reading too much into DRY. I pertains more to things that could be wrapped up in a function. I.e. instead of repeating the same fives lines all over the place wrap those 5 lines in a function and call the function everywhere you need it.
What you have as an example is just setting a value in a dictionary (the session object in this case), and that is the simplest way to store and retrieve objects in it.
I can't remember for the life of me where I humbly re-purposed this code from, but it's pretty nice:
using System;
using System.Web;
namespace Project.Web.UI.Domain
{
public abstract class SessionBase<T> where T : class, new()
{
private static readonly Object _padlock = new Object();
private static string Key
{
get { return typeof(SessionBase<T>).FullName; }
}
public static T Current
{
get
{
var instance = HttpContext.Current.Session[Key] as T;
lock (SessionBase<T>._padlock)
{
if (instance == null)
{
HttpContext.Current.Session[Key]
= instance
= new T();
}
}
return instance;
}
}
public static void Clear()
{
var instance = HttpContext.Current.Session[Key] as T;
if (instance != null)
{
lock (SessionBase<T>._padlock)
{
HttpContext.Current.Session[Key] = null;
}
}
}
}
}
The idea behind it two fold. The type created should be the only type you need. It's basically a big strongly-typed wrapper. So you have some object you want to keep extending information in:
public class MyClass
{
public MyClass()
public string Blah1 { get; set; }
}
Then down the road you extend MyClass and you don't want to have to remember all the Key Values, store them in AppSettings or Const variables in Static Classes. You simply define what you want to store:
public class MyClassSession : SessionBase<MyClass>
{
}
And anywhere in your program you simply use the class.
// Any Asp.Net method (webforms or mvc)
public void SetValueMethod()
{
MyClassSesssion.Current.Blah1 = "asdf";
}
public string GetValueMethod()
{
return MyClassSession.Current.Blah1;
}
Optionally you could place the access to this session object in a base page and wrap it in a property:
class BasePage : Page
{
...
public string MySessionObject
{
get
{
if(Session["myKey"] == null)
return string.Empty;
return Session["myKey"].ToString();
}
set
{
Session["myKey"] = value;
}
}
...
}
Here you are repeating the myKey string but it is encapsulated into the property. If you want to go to the extreme of avoiding this, create a constant with the key and replace the string.

What is better way to implement static global collections?

I am thinking what is the best way to load data to collections if it is global per application;
public static class ErrorValues
{
public static readonly Dictionary<int, string> errorInfo = new Dictionary<int, string>
{
{0, "Error 404"},
{1, "Error 500"},
{2, "Error 301"}
};
}
or like this
public static class ErrorValues
{
public static Dictionary<int, string> errorInfo;
static ErrorValues()
{
if (errorInfo == null)
{
errorInfo = LoadDataToDictionary();
}
}
}
better solutions? Why?
If your data is static, I recommend creating a meaningful type
Example:
public class ErrorValue
{
private static Dictionary<Int32, ErrorValue> _errors;
public static readonly ErrorValue Error404 = new ErrorValue(404, "Error 404");
public static readonly ErrorValue Error500 = new ErrorValue(500, "Error 500");
public static readonly ErrorValue Error301 = new ErrorValue(301, "Error 301");
public String ErrorName { get; private set; }
public Int32 ErrorCode { get; private set; }
private ErrorValue(Int32 errorCode, String errorName)
{
if (_errors == null)
_errors = new Dictionary<int, ErrorValue>();
ErrorName = errorName;
ErrorCode = errorCode;
_errors.Add(errorCode, this);
}
public static IEnumerable<ErrorValue> Errors { get { return _errors.Values; } }
public static ErrorValue GetErrorByCode(Int32 errorCode)
{
return _errors[errorCode];
}
}
This will lead to a less error-prone code due to type safety, since you can write methods with paramters of type ErrorValue:
void HandleError(ErrorValue ev)
{
// bla bla
}
Another benefit is that with this approach, you can easily extend the type; e.g. add other properties like Description, without huge changes to your code.
If you need similar static global collections, you can extract a common generic base class to provide methods like GetById or GetByName or similar.
There should be no difference in the generated IL except the beforefieldinit flag.
I think the first one is simple if items are static/hard coded and not to be loaded from DB or some other data source.
Second one is using singleton pattern that is used heavily in applications where object is to be created only once and reuse that object reference throughout the life cycle of application. and offers to initialize collection from any other data sources.
Conclusion: both are good but depends on what you need. personally i like the second way as it follows a design pattern.

C# Get property value without creating instance?

Is it possible to get value without creating an instance ?
I have this class:
public class MyClass
{
public string Name{ get{ return "David"; } }
public MyClass()
{
}
}
Now I need get the value "David", without creating instance of MyClass.
Real answer: no. It's an instance property, so you can only call it on an instance. You should either create an instance, or make the property static as shown in other answers.
See MSDN for more information about the difference between static and instance members.
Tongue-in-cheek but still correct answer:
Is it possible to get value without creating an instance ?
Yes, but only via some really horrible code which creates some IL passing in null as this (which you don't use in your property), using a DynamicMethod. Sample code:
// Jon Skeet explicitly disclaims any association with this horrible code.
// THIS CODE IS FOR FUN ONLY. USING IT WILL INCUR WAILING AND GNASHING OF TEETH.
using System;
using System.Reflection.Emit;
public class MyClass
{
public string Name { get{ return "David"; } }
}
class Test
{
static void Main()
{
var method = typeof(MyClass).GetProperty("Name").GetGetMethod();
var dynamicMethod = new DynamicMethod("Ugly", typeof(string),
Type.EmptyTypes);
var generator = dynamicMethod.GetILGenerator();
generator.Emit(OpCodes.Ldnull);
generator.Emit(OpCodes.Call, method);
generator.Emit(OpCodes.Ret);
var ugly = (Func<string>) dynamicMethod.CreateDelegate(
typeof(Func<string>));
Console.WriteLine(ugly());
}
}
Please don't do this. Ever. It's ghastly. It should be trampled on, cut up into little bits, set on fire, then cut up again. Fun though, isn't it? ;)
This works because it's using call instead of callvirt. Normally the C# compiler would use a callvirt call even if it's not calling a virtual member because that gets null reference checking "for free" (as far as the IL stream is concerned). A non-virtual call like this doesn't check for nullity first, it just invokes the member. If you checked this within the property call, you'd find it's null.
EDIT: As noted by Chris Sinclair, you can do it more simply using an open delegate instance:
var method = typeof(MyClass).GetProperty("Name").GetGetMethod();
var openDelegate = (Func<MyClass, string>) Delegate.CreateDelegate
(typeof(Func<MyClass, string>), method);
Console.WriteLine(openDelegate(null));
(But again, please don't!)
You can make that property static
public static string Name{ get{ return "David"; } }
Usage:
MyClass.Name;
You requirements do seem strange, but I think you're looking for some kind of metadata. You can use an attribute to achieve this:
public class NameAttribute : Attribute {
public string Name { get; private set; }
public NameAttribute(string name) {
Name = name;
}
}
[Name("George")]
public class Dad {
public string Name {
get {
return NameGetter.For(this.GetType());
}
}
}
[Name("Frank")]
public class Son : Dad {
}
public static class NameGetter {
public static string For<T>() {
return For(typeof(T));
}
public static string For(Type type) {
// add error checking ...
return ((NameAttribute)type.GetCustomAttributes(typeof(NameAttribute), false)[0]).Name;
}
}
Now this code can get names with and without instances:
Console.WriteLine(new Dad().Name);
Console.WriteLine(new Son().Name);
Console.WriteLine(NameGetter.For<Dad>());
Console.WriteLine(NameGetter.For<Son>());
You can make your property static, as pointed out by many others.
public static string Name{ get{ return "David"; } }
Be aware that this means your instances of MyClass will no longer have their own Name property, since static members belong to the class, not the individual object instances of it.
Edit:
In a note, you mentioned that you want to override the Name property in subclasses. At the same time, you want to be able to access it at the class level (access it without creating an instance of your class).
For the static properties, you would simply create a new Name property in each class. Since they are static, you're always (almost always, yay reflection) going to access them using a specific class, so you'd be specifying which version of Name you want to get. If you want to try and hack polymorphism in there and get the name from any given subclass of MyClass, you could do so using reflection, but I wouldn't recommend doing so.
Using the example from your comment:
public class Dad
{
public static string Name { get { return "George"; }
}
public class Son : Dad
{
public static string Name { get{ return "Frank"; }
}
public static void Test()
{
Console.WriteLine(Dad.Name); // prints "George"
Console.WriteLine(Son.Name); // prints "Frank"
Dad actuallyASon = new Son();
PropertyInfo nameProp = actuallyASon.GetType().GetProperty("Name");
Console.WriteLine(nameProp.GetValue(actuallyASon, null)); // prints "Frank"
}
As a side note, since you are declaring a property that has only a getter and it is returning a constant value, I recommend possibly using a const or static readonly variable instead.
public const string Name = "David";
public static readonly string Name = "David";
Usage for both would be the same:
string name = MyClass.Name;
The main benefit (and drawback) of const is that all references to it are actually replaced by its value when the code is compiled. That means it will be a little faster, but if you ever change its value, you will need to recompile ALL code that references it.
Whenever you write C# code, always check if your method and property getter/setter code does anything at all with other instance members of the class. If they don't, be sure to apply the static keyword. Certainly the case here, it trivially solves your problem.
The reason I really post to this question is that there's a bit of language bias at work in some of the answers. The C# rule that you can't call an instance method on a null object is a specific C# language rule. It is without a doubt a very wise one, it really helps to troubleshoot NullReferenceExceptions, they are raised at the call site instead of somewhere inside of a method where it gets very hard to diagnose that the this reference is null.
But this is certainly not a requirement to the CLR, nor of every language that run on the CLR. In fact, even C# doesn't enforce it consistently, you can readily bypass it in an extension method:
public static class Extensions {
public static bool IsNullOrEmpty(this string obj) {
return obj != null && obj.Length > 0;
}
}
...
string s = null;
bool empty = s.IsNullOrEmpty(); // Fine
And using your property from a language that doesn't have the same rule works fine as well. Like C++/CLI:
#include "stdafx.h"
using namespace System;
using namespace ClassLibrary1; // Add reference
int main(array<System::String ^> ^args)
{
MyClass^ obj = nullptr;
String^ name = obj->Name; // Fine
Console::WriteLine(name);
return 0;
}
Create a static property:
public class MyClass
{
public static string Name { get { return "David"; } }
public MyClass()
{
}
}
Get it like so:
string name1 = MyClass.Name;
That is not possible. As Name is an instance property, you can only get its value if you have an instance.
Also, note that you are not talking about a parameter, but about a property.
Create a static class or a static property, and you don't have to explicitly instantiate it.

Categories

Resources