views:

835

answers:

2

I'm reading this book on C# and .NET and I'm learning a bunch of cool stuff. I've read the part where the author talks about dynamically loading an assembly and creating an instance of a type in that assembly.

In AS3, it's possible to do the same kind of stuff, except for one thing : you can ask the compiler to not compile a set of class, but to check for type safety. Here's an example :

//Defined in an external library
public class A {...}


//In my application, I tell the compiler to type check A, but not compile it
var a:A = new A();
a.whatever();

At runtime in my application code, I can dynamically load my external library containing the definition of class A, load those definitions into my application's ApplicationDomain and everything will run fine. No needs of reflection!

Is this possible in C#?

In other words, can I instruct the C# compiler to typecheck against a bunch of class (let's say, in a library) but exclude them from compilation?

+2  A: 

I'm not 100% clear on what the as3 code is doing - but it sounds like you want to define a common interface (in a separate dll) that your external assembly can implement - and simply cast it when you create the object:

Type type = loadedAssembly.GetType(fullyQualifiedName);
IMyInterface obj = (IMyInterface)Activator.CreateInstance(type):

now you can use methods defined on obj easily.

Alternatively, in C# 4.0 the dynamic keyword provides duck-typing.

Marc Gravell
note: that the dynamic keyword is a little bit more flexible than the common interface, if you change the version on the common interface, you either need to recompile both the extension and the main app, or you need to deal with redirecting the resolver.
Sam Saffron
Is this the way you would do a plugin interface?
Subb
It is indeed one of the most common options for a plugin API.
Marc Gravell
@Subb, see MEF for the definitive Microsoft recommended *way* of doing plugins.
Sam Saffron
@Sam - which is currently only a *preview*. It *may in the future* be the definitive [etc].
Marc Gravell
@Marc, not according to Eric Lippert and another 11 stackoverflow members http://stackoverflow.com/questions/1070787/writing-c-plugin-system/1070801#1070801 , make sure you read my comments :p
Sam Saffron
Are you honestly arguing that it isn't currently "Preview 5"? My emphasis was on the future tense...
Marc Gravell
nope, I completely agree with you, was being sarcastic and it got lost in translation. Someone asked, how do I do XYZ with my plugin system, and the top upvoted answer was, don't worry about it, use MEF. Personally my problem with MEF is that it is really fat and solves all problems for everyone. But lately anywhere I hear plugins discussed, the answer tends to be use MEF.
Sam Saffron
+1  A: 

I just read this

Action Script is a dynamic language, it offers as a "special bonus" a type check feature, it helps you catch bugs at compile time, just like static typed languages do.

C# is a static-typed language, it does all it's type checking at compile time. The type check is not an "added bonus" its an integral feature. C# has always had the ability to late-bind using reflection and the feature is getting better with the new upcoming dynamic keyword.

However, if you use any of the late-binding features that C# has, you get no type checking.

Sam Saffron
Well. That's interesting. In my head, AS3 was a static-typed language with some dynamic features. I don't know why I was thinking this, since it based on ECMAScript, a dynamic language. I guess the compiler does a really good job at hiding it. That actually explain a lot of things...
Subb