473,466 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3795
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
5
by: heddy | last post by:
I understand that reflection allows me to discover the metadata of a class at runtime (properties, methods etc). What I don't understand is where this is useful. For example: If I am the sole...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
7
by: BK | last post by:
I've used reflection quite a bit to launch forms, it works great, requires little coding, and allows me to launch forms from a dynamic menu. Now I have a need to instantiate any one of several...
2
by: Tony Johansson | last post by:
Hello! I have a very simple example below. The problem is that I get the following compile error Error 1 Cannot implicitly convert type 'System.Reflection.MemberInfo' to 'MemberInfo'...
6
by: Cralis | last post by:
Hi guys, Someone once said, 'You can do that with reflection'. I can't recall what it was I was trying to do at the time, but then he said, 'Any developer knows what reflection is...'. I kept...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.