Re: Reflection



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
>


.



Relevant Pages

  • Re: Reflection, how to understand
    ... I'm trying understand jdk 5 reflection. ... Then it proceeds to get a constructor for said class, ... Then it proceeds to instantiate the object using said constructor, ... I use that code snippet for persistence of simple objects ...
    (comp.lang.java.programmer)
  • reflection c#
    ... constructor, how do i go about instantiating it. ... but for some odd reason i never am able to instantiate my objects ... - update redord or records ...
    (microsoft.public.dotnet.framework.remoting)
  • Reflection
    ... constructor, how do i go about instantiating it. ... but for some odd reason i never am able to instantiate my objects ... - update redord or records ...
    (microsoft.public.dotnet.csharp.general)
  • Reflection
    ... constructor, how do i go about instantiating it. ... but for some odd reason i never am able to instantiate my objects ... - update redord or records ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: DriverManager in JDBC
    ... Why the constructor is provided ??? ... ensured that no client can instantiate the class. ... It also prevents specious inheritance of the class. ... that return implementation instances by their interface type. ...
    (comp.lang.java.programmer)