473,387 Members | 3,033 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,387 software developers and data experts.

Reflection and Constructors

I am using reflection to load classes and I can't find any
examples on how to pass values to constructor parameters
using reflection, how is this done?

Here's an example of one of my methods, as you can see it
accepts two parameters. I want to pass those two values to
the constructor of the class I'm loading via reflection.

public IDbAdapter CreateAdapter(string query,
IDbConnection onnection)
{
string dbType = DB_ASSEMBLY;
string dbAdapterClass = Config.DbAdapterClass;

//load the assembly and class
IDbAdapter da =
(IDbAdapter)Assembly.Load(assemblyName).CreateInst ance(className);

return da;
}

Thanks in advance!
Nov 16 '05 #1
2 9282
V. Jenks <za******@bluebottle.com> wrote:
I am using reflection to load classes and I can't find any
examples on how to pass values to constructor parameters
using reflection, how is this done?


Either use Activator.CreateInstance (Type, object[]) or from the Type
reference, find the constructor you want (using Type.GetConstructor)
and then call Invoke on it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Here's a bit of an overview, though it focuses on calling internal
(non-public) constructors.

http://www.vbbox.com/blog/2004/05/cl...and-wacky.html

--
Klaus H. Probst, MVP
http://www.vbbox.com/
"V. Jenks" <za******@bluebottle.com> wrote in message
news:05****************************@phx.gbl...
I am using reflection to load classes and I can't find any
examples on how to pass values to constructor parameters
using reflection, how is this done?

Here's an example of one of my methods, as you can see it
accepts two parameters. I want to pass those two values to
the constructor of the class I'm loading via reflection.

public IDbAdapter CreateAdapter(string query,
IDbConnection onnection)
{
string dbType = DB_ASSEMBLY;
string dbAdapterClass = Config.DbAdapterClass;

//load the assembly and class
IDbAdapter da =
(IDbAdapter)Assembly.Load(assemblyName).CreateInst ance(className);

return da;
}

Thanks in advance!

Nov 16 '05 #3

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

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
9
by: A J Le Couteur Bisson | last post by:
Could someone please confirm that static class constructors are only called at the first use of a non-static constructor on the class, or am I doing something wrong? If this is indeed the case...
2
by: Carl Rosenberger | last post by:
Hi all, I would like to create an instance of a struct with reflection. The way I am used to doing this with classes: - get a ConstructorInfo from the Type - call Invoke() However, if a...
1
by: Adam W Root | last post by:
So I have a Product and an extending class ProductLine. I want to create a new ProductLine from a Product. I wrote this in a constructor: public Product(Product p) { foreach...
3
by: Boomessh | last post by:
Hai all, Can anyone there, help me in answering the following two questions. 1. What is the Maximum limit on the Constructors in C#? Can i have a class with out any constructors? 2....
3
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
2
by: Jason | last post by:
Hello, I"m trying to invoke a method using reflection on a DLL that is already within a project. However, I"m having a few problems and didn't know if anyone has any bright ideas. Please note,...
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...
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: 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
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...
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,...

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.