Simple reflection example?

R

Ryan

Hi all,

I've been banging my head on this for two days so I'd appreciate any
pointers from the knowledgable amongst you.

I have a win app that recurses through a path looking for assemblies to
load, it then attempts to load each file in turn and examine its
properties for a 'marker' property that defines it as a licencable
class. The amount of errors/misinformation on this subject is
staggering, every time I try a new approach I get another very rarely
documented error.

The classes that I want to examine all inherit from an abstract class
that defines a few standard properties for my licence. I've finally
managed to find the classes that inherit, but I cannot create instances
of them (they store some details that I want via read only properties).
The following code errors at the line indicated with invalid cast
(sorry it is VB, but you C# guys seem to know more reflection, any C#
samples would be cool too).

Sub DoIt(ByVal AssemblyPath As String, ByVal AssemblyType As
String)
Dim info As AppDomainSetup = New AppDomainSetup()

'// Set ApplicationBase to the current directory
info.ApplicationBase = "file:///" +
System.Environment.CurrentDirectory

'// Create an application domain with null evidence.

Dim dom As AppDomain =
AppDomain.CreateDomain("RemoteDomain", Nothing, info)

'// Load the assembly and instantiate the type.
Dim flags As BindingFlags = BindingFlags.Public Or
BindingFlags.Instance Or BindingFlags.CreateInstance

Dim objh As ObjectHandle =
dom.CreateInstanceFrom(AssemblyPath, AssemblyType, False, flags,
Nothing, Nothing, Nothing, Nothing, Nothing)

If (objh Is Nothing) Then
Console.WriteLine("CreateInstance failed")
Return
End If

'// Unwrap the object
Dim obj As Object = objh.Unwrap()

'// Cast to the actual type
' **** This line fails as the class cannot be cast to it's
inherited base type.
Dim h As MySoftware.Licencing.MyLicenceBase = CType(obj,
MySoftware.Licencing.MyLicenceBase)

'// Read the licence property.
Console.WriteLine(h.Name)

'// Clean up by unloading the application domain
AppDomain.Unload(dom)

End Sub


The app is used to licence classes at run time without prior knowledge
of the class being licenced.

The abstract class follow for info;

Imports System.Globalization
Imports System.ComponentModel

Namespace MySoftware.Licencing

<Serializable()> _
Public MustInherit Class MyLicenceBase

Sub New()
End Sub

Public MustOverride ReadOnly Property Title() As String
Public MustOverride ReadOnly Property Description() As String
Public MustOverride ReadOnly Property Name() As String
Public MustOverride ReadOnly Property Version() As Version
Public MustOverride ReadOnly Property UICulture() As
CultureInfo
Public ReadOnly Property LicenceGrouping() As String
Get
Return "XXX"
End Get
End Property

End Class

End Namespace

As I said, any pointers would be great, sorry for the long post.

Ryan
 
R

Ryan

I appreciate that it is VB sample code, but I'm fine with C# if anyone
wants to give an example in that. It is not the code I have the issue
with in general, it is getting it to run under any language. The C#
people tend to do more reflection, hence my preference to post here.

If it really upsets anyone, I'll move it on, but perhaps someone has a
C# angle on it?

Thanks

Ryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top