Reflection

A

Alexandre

Hi,

i currently know how to instantiate an object
which has no parameters in the constructor.

but i have objects which do have parameters in the
constructor, how do i go about instantiating it.

can some one please point me in the right direction?

i have looked into the activator, and into the
getconstructors as well as get parameters for methods and so on
but for some odd reason i never am able to instantiate my objects

the reason for my problem is this,

WHAT WORKS :

i am taking an object using reflection i am doing the following :

- insert into a data base
- create table
- create and break constraints
- drop tables
- delete from table
- update redord or records
....

WHAT DOES NOT WORK

i am not able to create my obects from a select...

i do my select have everything in the dataset
i iterate through the dataset yet i am unable

to instantiate my objects
my default constructor for my objects is as folows

constructor(int Id)
constructor(int Id, DateTime CreationTime)

the more im thinking about this problem
the less i see solutions and the more im thinking
of having the "int Id" removed and simply put as
a property like the reste of all the objects information....

if anyone has any ideas about this or leads thoughts solutions anything
please
help me out

Alexandre
 
J

Jon Skeet [C# MVP]

Alexandre said:
i currently know how to instantiate an object
which has no parameters in the constructor.

but i have objects which do have parameters in the
constructor, how do i go about instantiating it.

You can use Type.GetConstructors or Type.GetConstructor, and then call
Invoke on that with appropriate parameters.
 
A

Alexandre

i have been trying that approacha but it does not work so far, would
you mind if i posted some code to a paste bin ??
 
J

Jon Skeet [C# MVP]

Alexandre said:
i have been trying that approacha but it does not work so far, would
you mind if i posted some code to a paste bin ??

Absolutely - or just a short but complete program posted here would be
fine.
 
S

StealthyMark

You can use the line

object obj = Activator.CreateInstance(type, new object[] { id,
creationTime });

That's it! Alternatively you can use Reflection, but above method is
probably more secure:

using System;
using System.Reflection;

namespace YourNamespace
{
public class ObjectFactory
{
private static readonly Type[] parameterTypes = { typeof(int),
typeof(DateTime) };


public static object CreateObject(Type type, int id)
{
if (type == null)
throw new ArgumentNullException("type");

DateTime creationTime = DateTime.Now;

// find the constructor
ConstructorInfo constructor =
type.GetConstructor(parameterTypes);

if (constructor == null)
throw new ArgumentException("The type is not valid.",
"type");

// create and return the object
return constructor.Invoke(new object[] { id, creationTime });
}
}
}

Mark
 

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