471,350 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Simple reflection example?

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

Feb 22 '06 #1
2 3691
Ryan,
Your sampe code is in VB.NET, and this is the C# language newsgroup you've
posted to. Try the VB.NET language group.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Ryan" wrote:
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

Feb 22 '06 #2
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

Feb 22 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Derek Hart | last post: by
5 posts views Thread by heddy | last post: by
7 posts views Thread by BK | last post: by
2 posts views Thread by Tony Johansson | last post: by
6 posts views Thread by Cralis | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.