Re: Reflection
- From: "StealthyMark" <StealthyMark@xxxxxxxxxxxxx>
- Date: Mon, 11 Apr 2005 01:20:38 +0200
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
"Alexandre" <alexandre.brisebois@xxxxxxxxx> schrieb im Newsbeitrag
news:1112991635.624470.183750@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> 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
>
.
- References:
- Reflection
- From: Alexandre
- Reflection
- Prev by Date: Re: Collect the data
- Next by Date: Re: autorun maker
- Previous by thread: Re: Reflection
- Next by thread: what is the meaning of this syntax ? public ComboBoxExItem(: this("")
- Index(es):
Relevant Pages
|