I've got a static class with static getters.
public static class Cars
{
public static KeyValuePair<Guid, string> Acura
{
get { return new KeyValuePair<Guid, string>(new Guid("MMMMMMMM-509B-477A-ADB1-5CD014B41001"), "Acura"); }
}
public static KeyValuePair<Guid, string> AlfaRomeo
{
get { return new KeyValuePair<Guid, string>(new Guid("MMMMMMMM-509B-477A-ADB1-5CD014B41002"), "Alfa Romeo"); }
}
// etc.
}
I need to retrieve all the static properties from this static class and do something with each KeyValuePair. But the following throws a System.FormatException at runtime saying that it could not find recognizable digits
Type type = typeof(Cars);
foreach(var manufacturer in type.GetProperties())
{
if(manufacturer.PropertyType == typeof(KeyValuePair<Guid, string>))
{
var v = manufacturer.GetValue(null, null); //this does not work
// How to get the KeyValuePair<Guid, string>?
}
}
How to get each KeyValuePair?
UPDATE: Sorry.. the solution works perfectly, the problem is that GUID are not valuid Guids.. M is not hexadecimal character
This does not have to do anything with reflection or static properties. Within the getters of your properties there are exceptions thrown.
"MMMMMMMM-509B-477A-ADB1-5CD014B41001"
and "MMMMMMMM-509B-477A-ADB1-5CD014B41002"
are no valid Guid
s. Create Guid
s with valid values and the properties won't throw the exceptions.
Each of the digits in a Guid
must be a hexadecimal digit (see here ).
new Guid("MMMMMMMM-509B-477A-ADB1-5CD014B41001")
will throw the exception, while for example
new Guid("00000000-509B-477A-ADB1-5CD014B41001")
won't.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.