.NET

Reply
Valued Contributor
smcclure
Posts: 60
Registered: ‎06-08-2006
Message 1 of 6 (90 Views)

Registering Commands at Runtime

90 Views, 5 Replies
07-13-2006 10:50 AM
Hi,

I was wondering if it was possible to register a command at runtime under .NET. I can think of a few possible methods, but I am unsure how to get any of them working 100%. One option would be to wrap the ObjectARX function to add commands. I am not sure if this is very easy, though, given the nuances of calling managed code from AutoCAD. Another method may be to use a static initializer (aka a class constructor) to add the attribute dynamically when the class is first loaded (although I think this is not very easy either... I cannot find any way to access the type using static methods). Finally, there may be some managed code or even COM code that could accomplish this that I dont know of.

Any help or suggestions would be greatly appreciated.
-Scott Message was edited by: smcclure
*Tony Tanzillo
Message 2 of 6 (90 Views)

Re: Registering Commands at Runtime

07-13-2006 02:26 PM in reply to: smcclure
" I cannot find any way to access the type using static methods). "

Have you tried typeof(ClassName) ?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com
Valued Contributor
smcclure
Posts: 60
Registered: ‎06-08-2006
Message 3 of 6 (90 Views)

Re: Registering Commands at Runtime

07-13-2006 02:56 PM in reply to: smcclure
I should have explained in more detail... Yes, a simple GetType(ClassName) would work perfectly. Unfortunately, that defeats the purpose... What I am attempting to do is to create an abstract class for my commands, and that class would deal with marking an abstract method "Execute" as the CommandMethod. That way, when the Command class I am creating is inherited, the inheriting class doesnt have to mark the "Execute" method itself.

Therefore, since this code exists in the abstract class, I need to find a way to reference the type that is inheriting it, not the abstract class.

Therefore, I am not sure if it is possible for the base class to add an attribute to the inherited class before AutoCAD examines the assembly for attributes.

Is there any way to register a command without using attributes?

Thanks,
Scott
*Tony Tanzillo
Message 4 of 6 (90 Views)

Re: Registering Commands at Runtime

07-13-2006 03:55 PM in reply to: smcclure
You can use Assembly.GetExportedTypes() to iterate
all the class types in your assembly, and examine their
bases to determine if one derives from your abstract
class.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com

wrote in message news:5236743@discussion.autodesk.com...
I should have explained in more detail... Yes, a simple GetType(ClassName) would work perfectly. Unfortunately, that defeats the purpose... What I am attempting to do is to create an abstract class for my commands, and that class would deal with marking an abstract method "Execute" as the CommandMethod. That way, when the Command class I am creating is inherited, the inheriting class doesnt have to mark the "Execute" method itself.

Therefore, since this code exists in the abstract class, I need to find a way to reference the type that is inheriting it, not the abstract class.

Therefore, I am not sure if it is possible for the base class to add an attribute to the inherited class before AutoCAD examines the assembly for attributes.

Is there any way to register a command without using attributes?

Thanks,
Scott
Valued Contributor
smcclure
Posts: 60
Registered: ‎06-08-2006
Message 5 of 6 (90 Views)

Re: Registering Commands at Runtime

07-14-2006 05:42 AM in reply to: smcclure
That works very well, but now I realized that adding an attribute to a class at runtime is very difficult since they are apparently compiled into the code. The code using the getExportedTypes is below, but I need to figure out a way to register the class as a command or set the attribute.

[code]
Public Shared Sub New()
For Each type As System.Type In System.Reflection.Assembly.GetExecutingAssembly().GetExportedTypes()
If type.IsSubclassOf(GetType(Command)) Then
'Add the attribute or register the command...
End If
Next
End Sub
[/code] Message was edited by: smcclure
*Tony Tanzillo
Message 6 of 6 (90 Views)

Re: Registering Commands at Runtime

07-14-2006 11:12 AM in reply to: smcclure
I'm not going to say its impossible, but it certainly is not trivial or
easy to dynamically register commands. Just looking at some of
the internal code makes it clear that it involves a lot of unmanaged
code and several unmanaged classes.

You can't easily modify the attributes of a compiled class either.
The only way to achieve that is to dynamically generate the type
itself, which is not simple.

Perhaps you can cite why you need to dynamically register
commands?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com

wrote in message news:5237202@discussion.autodesk.com...
That works very well, but now I realized that adding an attribute to a class at runtime is very difficult since they are apparently compiled into the code. The code using the getExportedTypes is below, but I need to figure out a way to register the class as a command or set the attribute.

[code]
Public Shared Sub New()
For Each type As System.Type In System.Reflection.Assembly.GetExecutingAssembly().GetExportedTypes()
If type.IsSubclassOf(GetType(Command)) Then
'Add the attribute or register the command...
End If
Next
End Sub
[/code]

Message was edited by: smcclure
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.