310 likes | 735 Views
Using .NET 4.0 with AutoCAD. Albert Szilvasy Software Architect, AutoCAD Team. Agenda. The basics Getting AutoCAD to boot .NET 4.0 Migrating apps Using new features now Type embedding, late binding in C# Dynamic Language Runtime Script Hosting Into the future
E N D
Using .NET 4.0 with AutoCAD Albert Szilvasy Software Architect, AutoCAD Team
Agenda • The basics • Getting AutoCAD to boot .NET 4.0 • Migrating apps • Using new features now • Type embedding, late binding in C# • Dynamic Language Runtime • Script Hosting • Into the future • Implementing a dynamic type • More scriptable .NET API
Booting .NET 4.0 in AutoCAD • .NET 4.0 does not automatically roll forward .NET 2.0 apps • Edit acad.exe.config and add the following <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0"/> </startup> • .NET 4.0 Beta 2 is NOT compatible with AutoCAD 2010 • working with Microsoft to fix some issues • possibly some workarounds in Update 2
Migrating C#/VB.NET apps • Simply open project in Visual Studio 2010 • Set Target Framework to .NET 4.0 • Client Profile will NOT work
Migrating mixed mode apps • Trickier • C++ compiler/linker C/C++ runtime/ATL/MFC must stay on Visual Studio 2008 level • Linker errors due to mismatching metadata • Possible • Edit cl.exe.config and link.exe.config to bind them to .NET 4.0 • Apply workaround to avoid mismatching metadata error • #ifdef __CLR_VER • #if __CLR_VER > 20050727 • #error We can remove this workaround now. • #endif • #else • #define _TP_CALLBACK_ENVIRON _TP_CALLBACK_ENVIRON_workaround • #endif
Type embedding • Compiler embeds the interop types into your dll • Only the types that you actually use • Works for both VB and C# • Helpful if you use COM interop assemblies • Avoids having to redistribute interop assemblies for Office, other apps • Reduces the number of failure points • Easier deployment
Late binding in C# • VB.NET has always had this feature • C# programmers had to use reflection • Slow and cumbersome • New dynamic keyword
Late binding in C# • dynamic only exists at compile time, dynamic = object at runtime • Advantages • Easier to support incompatible versions of AutoCAD • Allows interop with scripting languages (Python, Ruby) • Disadvantage • No compile time checks, every error is a runtime error • Slightly slower
Dynamic Language Runtime • Open source project and it is also part of .NET 4.0 • Open source: http://www.codeplex.com/dlr • .NET 4.0: System.Core.dll • Built on top of the CLR • Key benefits • Makes it easier to implement a ‘dynamic’ language: • Python, Ruby (by Microsoft) • Scheme, a LISP dialect (http://ironscheme.codeplex.com/) • Dynamic features in ‘static’ languages (dynamic keyword in C#) • Enables interop between static and dynamic languages • C#/VB.NET classes can exhibit dynamic behavior • Common hosting model for dynamic languages
Script Hosting • DLR makes is easy to execute script written in any language that supports the DLR • The host can provide entry points: • ‘Application’ • ‘ThisDrawing’
Scripts: Pluses and Minuses • Advantages • No build, restart cycle: good for quick experimentation • Easy, safe way to share scripts in clear text • Lot of languages are available • Python, ruby, scheme (today) • Vb.net, C#, others (years to come) • Full power of .NET/COM object model • Easier to maintain compatibility • Disadvantages • No integrated debugging (yet) • No intellisense (yet) • Just like LISP before VLIDE
Exposing dynamic behavior • DLR allows applications to expose dynamic behavior • Why would you do this? • Provide convenient, lightweight syntax • Consider:
Implementing a dynamic object • Multiple levels of engagement • Simple property bag: ExpandoObject • You simply instantiate this type • Easy and powerful: DynamicObject • Derive from this type • Does not work for value types • Full power but more work: DynamicMetaObject • Any type can implement IDynamicMetaObjectProvider • Must deal with Expression Trees (Abstract Syntax Tree)
Making a smarter ObjectId • Implement IDynamicMetaObjectProvider • ObjectId “knows” the type of the object that it points to (see ObjectClass property) • ObjectId can expose all properties/method of the underlying object and inject Open/Close • Syntactically more pleasing • Easier to write correct code • Easy to implement, does not require advance knowledge properties/methods • Performance overhead is minimized by DLR callsite caching
Expression Trees • Programs can be represented as trees operations (Abstract Syntax Trees) • A = 1+1 • DynamicMetaObject allows you to build Expression Trees on the fly and insert them into the AST generated by the compiler • DLR compiles the Expression Tree into IL = A + 1 1
Disclaimer • Remember .NET 4.0 is still in beta • Lots of forward looking stuff here • Do NOT count on anything presented appear in shipping product
Other resources • CP208-1: Developing for AutoCAD Using IronPython and IronRuby – Wednesday 10-11:30am • http://www.codeplex.com/dlr • http://www.codeplex.com/IronPython