Type.GUID Replacement

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
 
P

Peter Foot

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
 
M

MarkusSchaber

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
 
M

MarkusSchaber

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top