interfaces and GUIDS
-
Can anyone help? I want to code an interface and assign a GUID to it. public interface IMyInterface { } On the web I found articles that say I should put the interfaces GUID above the interface declaration. But I get an error message saying GUID is not recognised. My using clause has System. So how can I do this? The reason is in Delphi my objects support interfaces and then by zooming thru a list of objects I can ask if the object supports the interface and if so talk to the object thru that interface. I have had a good look thru my books and internet articles but cannot find any really good info. If there is a new way to do this I would also be interested. Thanks Luke
-
Can anyone help? I want to code an interface and assign a GUID to it. public interface IMyInterface { } On the web I found articles that say I should put the interfaces GUID above the interface declaration. But I get an error message saying GUID is not recognised. My using clause has System. So how can I do this? The reason is in Delphi my objects support interfaces and then by zooming thru a list of objects I can ask if the object supports the interface and if so talk to the object thru that interface. I have had a good look thru my books and internet articles but cannot find any really good info. If there is a new way to do this I would also be interested. Thanks Luke
Yes, you do use the
GuidAttribute
but you must use the right namespace:System.Runtime.InteropServices
. You can either include it toward the top with theusing
statement, or prefix yourGuidAttribute
(or any class for that matter) with the proper namespace. You must also be referencing the assembly in which the Type contains. I'm only mentioning this so you understand. TheSystem.Runtime.InteropServices.GuidAttribute
Type is in the mscorlib.dll assembly, so it is automatically referenced by the compiler (unless it's told otherwise, which is only possible with the command-line compiler).-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Yes, you do use the
GuidAttribute
but you must use the right namespace:System.Runtime.InteropServices
. You can either include it toward the top with theusing
statement, or prefix yourGuidAttribute
(or any class for that matter) with the proper namespace. You must also be referencing the assembly in which the Type contains. I'm only mentioning this so you understand. TheSystem.Runtime.InteropServices.GuidAttribute
Type is in the mscorlib.dll assembly, so it is automatically referenced by the compiler (unless it's told otherwise, which is only possible with the command-line compiler).-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
is that above the interface declaration System.Runtime.InteropServices.GuidAttribute["....."] public interface IMyinterface { } And how can I ask an object if it supports the interface so that I can talk to it soley thru that interface (this is how I would have done it in Delphi) Cheers Luke
-
is that above the interface declaration System.Runtime.InteropServices.GuidAttribute["....."] public interface IMyinterface { } And how can I ask an object if it supports the interface so that I can talk to it soley thru that interface (this is how I would have done it in Delphi) Cheers Luke
No offense meant, but you should probably read the C# specification. Attributes are like so (fully-qualified example):
[System.Runtime.InteropServices.Guid("363EBCCC-5E9A-4e14-8A5D-F0DA44E69EF9")]
public interface IMyInterface
{
{In C# (and some other languages, but you have to read their specs to find out), you can drop the "Attribute" at the end of attributes. If you want to use properties that aren't in the constructor parameters, fill all the necessary params then use
PropertyName=Value
, like so:[MadeUpAttribute("somestring", SomeParam="SomeValue")]
public class SomeClass
{
}-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
No offense meant, but you should probably read the C# specification. Attributes are like so (fully-qualified example):
[System.Runtime.InteropServices.Guid("363EBCCC-5E9A-4e14-8A5D-F0DA44E69EF9")]
public interface IMyInterface
{
{In C# (and some other languages, but you have to read their specs to find out), you can drop the "Attribute" at the end of attributes. If you want to use properties that aren't in the constructor parameters, fill all the necessary params then use
PropertyName=Value
, like so:[MadeUpAttribute("somestring", SomeParam="SomeValue")]
public class SomeClass
{
}-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
No offence taken. I have brought 2 very good reference texts for C# / .NET. My problem is that I professionally program in Delphi. So I know what I want to do but I haven't yet got used to framing the question I want to ask in C# terms to get the answer. But moving to C# has been very easy due to the similar class libraries so I have a head start on my fellow C++ m8s. Thanks for your help tho.
-
No offence taken. I have brought 2 very good reference texts for C# / .NET. My problem is that I professionally program in Delphi. So I know what I want to do but I haven't yet got used to framing the question I want to ask in C# terms to get the answer. But moving to C# has been very easy due to the similar class libraries so I have a head start on my fellow C++ m8s. Thanks for your help tho.
My comment wasn't in reference to how you "framed" your question, but rather your syntax. Attributes in C# are enclused in square brackets ([ and ]) and you tried using them for your constructor params, which are always parens (( and )). This is just a simple matter of syntax. While it's true that the syntax (i.e., language) you use to write your code for .NET isn't really so important (knowing how to use and extend the base class library, and how to make your own libraries and applications is more important), you still need to know the syntax of the language you've choosen. I was just pointing out that you should read over the C# language specifications to know exactly what the syntax is - you might even learn a few tricks that aren't always discussed in books (like using the
using
statement to automatically disposeIDisposable
implementations, or to use the same statement to creat a Type alias (especially handy when you have Type collisions, i.e. two different Types with the same name that belong to two or more namespaces you've imported).-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
My comment wasn't in reference to how you "framed" your question, but rather your syntax. Attributes in C# are enclused in square brackets ([ and ]) and you tried using them for your constructor params, which are always parens (( and )). This is just a simple matter of syntax. While it's true that the syntax (i.e., language) you use to write your code for .NET isn't really so important (knowing how to use and extend the base class library, and how to make your own libraries and applications is more important), you still need to know the syntax of the language you've choosen. I was just pointing out that you should read over the C# language specifications to know exactly what the syntax is - you might even learn a few tricks that aren't always discussed in books (like using the
using
statement to automatically disposeIDisposable
implementations, or to use the same statement to creat a Type alias (especially handy when you have Type collisions, i.e. two different Types with the same name that belong to two or more namespaces you've imported).-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Im my opinion, a good example of the uses of attributes is when it comes to describing properties for the property explorer in Visual Studio.net.
using System;
using System.ComponentModel;namespace Examples
{
public class ExampleControl : UserControl
{
private IndentStyle m_eExampleProperty = IndentStyle.Smart;
[
Category("Design"),
Description("Gets/sets an example value."),
DefaultValue(IndentStyle.Smart),
Browsable(true)
]
public IndentStyle ExampleProperty
{
get
{
return m_eExampleProperty;
}
set
{
m_eExampleProperty = value;
}
}
}
}This would produce this kind of output:
If you want to try this out, just cut-and-paste the code into a source file, and create an instance of the control on the designer form. - Daniël Pelsmaeker
Microsoft is to quality software what McDonalds is to gourmet cooking