すべてのプロパティの値をリセットする静的クラスでメソッドを作成できます。静的クラスがあると考えてください
public static class ClassA
{
public static int id=0;
public static string name="";
public static void ResetValues()
{
// Here you want to reset to the old initialized value
id=0;
name="";
}
}
これで、他のクラスから以下のアプローチのいずれかを使用して、静的クラスの値をリセットできます
アプローチ 1 - 直接呼び出す
ClassA.ResetValues();
アプローチ 2 - 既知の名前空間と既知のクラスからメソッドを動的に呼び出す
Type t1 = Type.GetType("Namespace1.ClassA");
MethodInfo methodInfo1 = t1.GetMethod("ResetValues");
if (methodInfo1 != null)
{
object result = null;
result = methodInfo1.Invoke(null, null);
}
アプローチ 3 - アセンブリ/アセンブリのセットからメソッドを動的に呼び出す
foreach (var Ass in AppDomain.CurrentDomain.GetAssemblies())
{
// Use the above "If" condition if you want to filter from only one Dll
if (Ass.ManifestModule.FullyQualifiedName.EndsWith("YourDll.dll"))
{
List<Type> lstClasses = Ass.GetTypes().Where(t => t.IsClass && t.IsSealed && t.IsAbstract).ToList();
foreach (Type type in lstClasses)
{
MethodInfo methodInfo = type.GetMethod("ResetValues");
if (methodInfo != null)
{
object result = null;
result = methodInfo.Invoke(null, null);
}
}
break;
}
}