Hi,
I have structure like this:
namespace Namespace
{
public class User
{
public Subnamespace.Se ttings Settings = new Subnamespace.Se ttings();
}
namespace Subnamespace
{
public class Settings
{
public string MyValue { get { return "Hello from there"; } }
}
public static class Helper
{
public static User User = new User();
}
}
}
now, I would like to get the value from string
"Namespace.Subn amespace.Helper .User.Settings. MyValue".
Reinout Waelput gave me on MSDN forums this code:
string Location = "MyNamespace.My Class.TheStruct .TheStruct2.MyV alue";
string s = Location.Substr ing(0, Location.IndexO f("."));
Location = Location.Substr ing(Location.In dexOf(".") + 1);
while (Type.GetType(s ) == null)
{
s = string.Concat(s , ".", Location.Substr ing(0, Location.IndexO f(".")));
Location = Location.Substr ing(Location.In dexOf(".") + 1);
}
System.Type pageType = Type.GetType(s) ;
System.Reflecti on.FieldInfo myField;
while (Location.Index Of(".") >= 1)
{
string str = Location.Substr ing(0, Location.IndexO f("."));
Location = Location.Substr ing(Location.In dexOf(".") + 1);
myField = pageType.GetFie ld(str);
pageType = myField.GetValu e(pageType).Get Type();
}
pageType.GetFie ld(Location, System.Reflecti on.BindingFlags .GetField);
Which works well, however fails in my case, probably because of the
Subnamespace.Se ttings declaration. The exception is:
System.Argument Exception was unhandled
Field 'Settings' defined on type 'Namespace.User ' is not a field on the
target object which is of type 'System.Runtime Type'.
at System.Reflecti on.RtFieldInfo. CheckConsistenc y(Object target)
at System.Reflecti on.RtFieldInfo. InternalGetValu e(Object obj, Boolean
doVisibilityChe ck, Boolean doCheckConsiste ncy)
at System.Reflecti on.RtFieldInfo. GetValue(Object obj)
VisualStudio during debug, when selecting
Namespace.Subna mespace.Helper. User.Settings.M yValue have no troubles.
Any idea how to handle the cross namespace declaration?
Thanks a lot,
Jan
I have structure like this:
namespace Namespace
{
public class User
{
public Subnamespace.Se ttings Settings = new Subnamespace.Se ttings();
}
namespace Subnamespace
{
public class Settings
{
public string MyValue { get { return "Hello from there"; } }
}
public static class Helper
{
public static User User = new User();
}
}
}
now, I would like to get the value from string
"Namespace.Subn amespace.Helper .User.Settings. MyValue".
Reinout Waelput gave me on MSDN forums this code:
string Location = "MyNamespace.My Class.TheStruct .TheStruct2.MyV alue";
string s = Location.Substr ing(0, Location.IndexO f("."));
Location = Location.Substr ing(Location.In dexOf(".") + 1);
while (Type.GetType(s ) == null)
{
s = string.Concat(s , ".", Location.Substr ing(0, Location.IndexO f(".")));
Location = Location.Substr ing(Location.In dexOf(".") + 1);
}
System.Type pageType = Type.GetType(s) ;
System.Reflecti on.FieldInfo myField;
while (Location.Index Of(".") >= 1)
{
string str = Location.Substr ing(0, Location.IndexO f("."));
Location = Location.Substr ing(Location.In dexOf(".") + 1);
myField = pageType.GetFie ld(str);
pageType = myField.GetValu e(pageType).Get Type();
}
pageType.GetFie ld(Location, System.Reflecti on.BindingFlags .GetField);
Which works well, however fails in my case, probably because of the
Subnamespace.Se ttings declaration. The exception is:
System.Argument Exception was unhandled
Field 'Settings' defined on type 'Namespace.User ' is not a field on the
target object which is of type 'System.Runtime Type'.
at System.Reflecti on.RtFieldInfo. CheckConsistenc y(Object target)
at System.Reflecti on.RtFieldInfo. InternalGetValu e(Object obj, Boolean
doVisibilityChe ck, Boolean doCheckConsiste ncy)
at System.Reflecti on.RtFieldInfo. GetValue(Object obj)
VisualStudio during debug, when selecting
Namespace.Subna mespace.Helper. User.Settings.M yValue have no troubles.
Any idea how to handle the cross namespace declaration?
Thanks a lot,
Jan
Comment