473,385 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Reflection from System assemblies

Hi

I'm trying to create an instance of an object that's in one of the System
assemblies; this code works fine for classes in my application, but fails
when the assembly is not in the application directory, e.g when it's in the
GAC.

Private Function Create(ByVal className As String) As Object
Dim handle As System.Runtime.Remoting.ObjectHandle

' Now create it
Try
handle = Activator.CreateInstance(AssemblyName, Me.Namespace + "."
+ ClassPrefix + className)
' Unwrap the object and return it
Return CType(handle.Unwrap, Object)
Catch ex As Exception
Throw New Exception("Error occured creating class from " +
Me.Namespace + "." + className + " in " + AssemblyName + " - " +
ex.ToString, ex)
End Try
End Function

Any ideas how to work around this?

Paul
Nov 20 '05 #1
2 830
Paul,

* "Paul Hatcher" <ph******@spamless.cix.co.uk> scripsit:
I'm trying to create an instance of an object that's in one of the System
assemblies; this code works fine for classes in my application, but fails
when the assembly is not in the application directory, e.g when it's in the
GAC.


The code below worked for me even with Windows Forms's 'Button' class:

\\\
Private Function CreateClassByName( _
ByVal PartialAssemblyName As String, _
ByVal QualifiedClassName As String _
) As Object
Return _
Activator.CreateInstance( _
[Assembly].LoadWithPartialName( _
PartialAssemblyName _
).GetType(QualifiedClassName) _
)
End Function
///

--
Herfried K. Wagner [MVP]
<http://dotnet.mvps.org/>
Nov 20 '05 #2
Herfried

Thanks, that worked fine - I started using the version I posted about 2
years ago and it's always worked, but it's been used to create forms
typically.

Paul

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:c1*************@ID-208219.news.uni-berlin.de...
Paul,

* "Paul Hatcher" <ph******@spamless.cix.co.uk> scripsit:
I'm trying to create an instance of an object that's in one of the System assemblies; this code works fine for classes in my application, but fails when the assembly is not in the application directory, e.g when it's in the GAC.


The code below worked for me even with Windows Forms's 'Button' class:

\\\
Private Function CreateClassByName( _
ByVal PartialAssemblyName As String, _
ByVal QualifiedClassName As String _
) As Object
Return _
Activator.CreateInstance( _
[Assembly].LoadWithPartialName( _
PartialAssemblyName _
).GetType(QualifiedClassName) _
)
End Function
///

--
Herfried K. Wagner [MVP]
<http://dotnet.mvps.org/>

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Frazer | last post by:
hi could any one tell me which real life senarios reflection can be used in ? thnx
2
by: MarkB | last post by:
I am new to using reflection, and I can't understand why this code fails for sClass="CheckBox" 'get loaded assemblies oAssemblies = AppDomain.CurrentDomain.GetAssemblies() 'find our type in...
12
by: Antony | last post by:
Hello - I am wanting to print out a "Yes" next to classes that implement "Interface01", otherwise a "No". Here is my code. It crashes with a null reference exception and I am not sure why. Ideas?...
2
by: Jeff | last post by:
I am trying to dynamically load an assembly via reflection and then invoke a method of that assembly that will populate a custom type collection passed into the method byref. I am able to...
9
by: TS | last post by:
i have code that creates assemblies and classes from the assemlby and methods from the classes to set properties of dynamically created controls. How i should go about validating the assemblies,...
7
by: Allan Ebdrup | last post by:
How do I get hold of the type when I have a string that represents the type. For example I have the string "OFiR.Recruitment.Department" And I want to get the type called...
6
by: Larry | last post by:
When compiling, i get the following error. Preparing resources... Updating references... Performing main compilation... vbc : error BC30145: Unable to emit assembly: Referenced assembly...
0
by: npthomson | last post by:
Hi all, This could get a bit complicated but I'll try to be as clear as possible. I've written an application that discovers plugins at runtime using reflection from a subdirectory of the...
3
by: groovyjman21 | last post by:
Hi, I am using FXCop on my c# project and it is complaining about signing the assembly with a strong name key. All the documentation I can find says to do: using System.Reflection; .... ...
4
by: Per Bolmstedt | last post by:
(This question has been asked previously in this group, but I don't think it was ever really properly answered.) I want to use reflection - preferably - to find all Web Forms in my web site that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.