.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Registering Commands at Runtime

5 REPLIES 5
Reply
Message 1 of 6
smcclure
308 Views, 5 Replies

Registering Commands at Runtime

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
5 REPLIES 5
Message 2 of 6
Anonymous
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
Message 3 of 6
smcclure
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
Message 4 of 6
Anonymous
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
Message 5 of 6
smcclure
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
Message 6 of 6
Anonymous
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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost