Type.GUID Replacement

  • Thread starter Thread starter MarkusSchaber
  • Start date Start date
M

MarkusSchaber

Hello,

How do I get the Guid for a class in the compact framework (the one
that was defined with the GuidAttribute from
System.Runtime.InteropServices)?

Or is there an alternate way (possibly compatible with the desktop
framework) to add a GUID to a compact framework type?

Thanks,
Markus
 
Assuming the Type is a COM Interface/Class wrapper it will have been give a
GuidAttribute so the following will work:-

Guid typeGuid = Guid.Empty;
Type interfaceType = myObject.GetType().GetInterfaces()[0];
object[] attributes = interfaceType.GetCustomAttributes(false);
foreach(object o in attributes)
{
if(o is GuidAttribute)
{
//has GuidAttribute - get the value
GuidAttribute ga = (GuidAttribute)o;
typeGuid = new Guid(ga.Value);
}
}

For simplicity the code here only gets the first implemented interface so
you may need to change this code depending on how you are getting the
object/type in the first place but you can see the logic to get the Guid -
look through the custom attributes of the type and if you find a
GuidAttribute get its value (which will be a guid as a string) and create a
new Guid object from it.

Peter
 
Hi, Peter,

Assuming the Type is a COM Interface/Class wrapper it will have been give a
GuidAttribute so the following will work:-
[...]

For simplicity the code here only gets the first implemented interface so
you may need to change this code depending on how you are getting the
object/type in the first place but you can see the logic to get the Guid -
look through the custom attributes of the type and if you find a
GuidAttribute get its value (which will be a guid as a string) and create a
new Guid object from it.

In my case, we assign the GUID directly to a class (it's not for COM
purposes), but I'll try whether it works this way...

Thanks,
Markus
 
Hi, Peter,

In my case, we assign the GUID directly to a class (it's not for COM
purposes), but I'll try whether it works this way...

It seems to work now, thanks.

Markus
 
Back
Top