I use one third party dll and make some function call.
Sometimes this function call fails. So I reinitialize the object and again make the function call. However, it still fails. If I stop my code execution and restart it, my function call again works fine.
I used a decompiler over this third party dll and realized that internally in the dll's class, there is a static object which is responsible for this exception. When I restart my program, the static object has to reinitialize and then my code works. Is it possible that I can force this using code? Basically, I want to ensure that even the static objects inside the class inside the dll get reinitialize when I make my object. I cannot restart my .NET program in production.
You can use Reflection library to do it:
var prop = s.GetType().GetField("ThePrivateStaticObjectName",
System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Instance);
prop.SetValue(s, "new value");
and a generic method to do it for any objects:
public static void SetPrivatePropertyValue<T>(T obj, string propertyName, object newValue)
{
// add a check here that the object obj and propertyName string are not null
foreach (FieldInfo fi in obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic))
{
if (fi.Name.ToLower().Contains(propertyName.ToLower()))
{
fi.SetValue(obj, newValue);
break;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With