System.Reflection Activator.CreateInstance

G

Guest

**************************************
//Load the Assembly
Assembly a = Assembly.LoadFrom(sAssembly);

//Get Types so we can Identify the Interface.
Type[] mytypes = a.GetTypes();
BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);

//Iterate through the Assembly to find Class with a Public Interface.
//****Each Assembly should only have one Public Entry Point****
//****using this method to expose that method and pass the XML data to it****

foreach (Type t in mytypes)
{
MethodInfo[] mi = t.GetMethods(flags);
Object obj = Activator.CreateInstance(t);

**************************************

I am trying to dynamically call an assembly. The Assembly name is being
pulled from a SharePoint List and in this case it is called TransformXML. I
am getting the following error when it gets to the Activator...

ERROR:
**********************************************************
*An unhandled exception of type 'System.MissingMethodException' occurred in
* *mscorlib.dll
*
*
*
*Additional information: No parameterless constructor defined for this
object *
**********************************************************



I am also including the code from the class being called.

Transform.dll
**************************************************
public class CreateTransform
{
#region Public Methods

public CreateTransform()
{
}

public CreateTransform(string sXML, Microsoft.SharePoint.SPListItem
_spListItem)
{
//System.Xml.XmlDocument xmlDoc;

Microsoft.SharePoint.SPAttachmentCollection spAttachColl =
this.GetXSLTransform(_spListItem);

this.TransformXML(sXML,spAttachColl);

}

#endregion
 
C

Cordell Lawrence

The exception is thrown because the type that you're attempting to creating
an instace of does not have a public, parameterless constructor [ That was
already said in your exception dump ]. Advise, you should probably do some
more checking on the type that you're attempting to create an instance of
before calling the Activator.CreateInstance( ) method.

Some checks may include
- Ensure that the type is not abstract
- Ensure that the type is not an interface ( this one is taken care of if
you check of abstract types ) or enum
- Ensure that the type has a public parameterless constructor
[All of this is predicated on the fact that you really want to instiate and
invoke the methods on your object in this way]

Hope this helps.
Cordell Lawrence [[email protected]]
Teleios Systems Ltd.

"System.Reflection Activator" <System.Reflection
(e-mail address removed)> wrote in message
news:[email protected]...
 
G

Guest

I was under the impression that creating the method CreateTransform() that
contains no parameters would be considered the default contructor. Since it
does not require any parameters I am still confused as to what the problem
is. Could you please elaborate?


public class CreateTransform
{
#region Public Methods
public CreateTransform()
{
}

public CreateTransform(string sXML, Microsoft.SharePoint.SPListItem
_spListItem)
{
//System.Xml.XmlDocument xmlDoc;

Microsoft.SharePoint.SPAttachmentCollection spAttachColl =
this.GetXSLTransform(_spListItem);

this.TransformXML(sXML,spAttachColl);

}

#endregion


Cordell Lawrence said:
The exception is thrown because the type that you're attempting to creating
an instace of does not have a public, parameterless constructor [ That was
already said in your exception dump ]. Advise, you should probably do some
more checking on the type that you're attempting to create an instance of
before calling the Activator.CreateInstance( ) method.

Some checks may include
- Ensure that the type is not abstract
- Ensure that the type is not an interface ( this one is taken care of if
you check of abstract types ) or enum
- Ensure that the type has a public parameterless constructor
[All of this is predicated on the fact that you really want to instiate and
invoke the methods on your object in this way]

Hope this helps.
Cordell Lawrence [[email protected]]
Teleios Systems Ltd.

"System.Reflection Activator" <System.Reflection
(e-mail address removed)> wrote in message
**************************************
//Load the Assembly
Assembly a = Assembly.LoadFrom(sAssembly);

//Get Types so we can Identify the Interface.
Type[] mytypes = a.GetTypes();
BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);

//Iterate through the Assembly to find Class with a Public Interface.
//****Each Assembly should only have one Public Entry Point****
//****using this method to expose that method and pass the XML data to it****

foreach (Type t in mytypes)
{
MethodInfo[] mi = t.GetMethods(flags);
Object obj = Activator.CreateInstance(t);

**************************************

I am trying to dynamically call an assembly. The Assembly name is being
pulled from a SharePoint List and in this case it is called TransformXML. I
am getting the following error when it gets to the Activator...

ERROR:
**********************************************************
*An unhandled exception of type 'System.MissingMethodException' occurred in
* *mscorlib.dll
*
*
*
*Additional information: No parameterless constructor defined for this
object *
**********************************************************



I am also including the code from the class being called.

Transform.dll
**************************************************
public class CreateTransform
{
#region Public Methods

public CreateTransform()
{
}

public CreateTransform(string sXML, Microsoft.SharePoint.SPListItem
_spListItem)
{
//System.Xml.XmlDocument xmlDoc;

Microsoft.SharePoint.SPAttachmentCollection spAttachColl =
this.GetXSLTransform(_spListItem);

this.TransformXML(sXML,spAttachColl);

}

#endregion
 
C

Cordell Lawrence

Yeah you are right, your type CreareTransform does fulfill that criteria,
but what I was saying was that the type that you were attempting to create
at that point (which was not the one you created) didn't have a default
constructor. See, when you execute a Assembly.GetTypes() it returns all the
types defined in that assembly, inculding interfaces and enums etc. You code
is iterating through this list impartially and attempting to create
instances to those types.

To illustrare try this out.

/* UNTESTED CODE BLOCK */
....
Assembly asm = Assembly.LoadFrom ( fileName );
Type[] asmTypes = asm.GetTypes ( );

foreach ( Type t in asmTypes )
Console.WriteLine(t.FullName);

// object o = Activator.CreateInstance ( t );
....

Check out the types being written to the cosole / debug window and you'll
see what you're attempting to instantiate (which is the type without the
default parameterless constructor.

HTH
Cordell Lawrence
Teleios Systems Ltd.


brian c said:
I was under the impression that creating the method CreateTransform() that
contains no parameters would be considered the default contructor. Since it
does not require any parameters I am still confused as to what the problem
is. Could you please elaborate?


public class CreateTransform
{
#region Public Methods
public CreateTransform()
{
}

public CreateTransform(string sXML, Microsoft.SharePoint.SPListItem
_spListItem)
{
//System.Xml.XmlDocument xmlDoc;

Microsoft.SharePoint.SPAttachmentCollection spAttachColl =
this.GetXSLTransform(_spListItem);

this.TransformXML(sXML,spAttachColl);

}

#endregion


Cordell Lawrence said:
The exception is thrown because the type that you're attempting to creating
an instace of does not have a public, parameterless constructor [ That was
already said in your exception dump ]. Advise, you should probably do some
more checking on the type that you're attempting to create an instance of
before calling the Activator.CreateInstance( ) method.

Some checks may include
- Ensure that the type is not abstract
- Ensure that the type is not an interface ( this one is taken care of if
you check of abstract types ) or enum
- Ensure that the type has a public parameterless constructor
[All of this is predicated on the fact that you really want to instiate and
invoke the methods on your object in this way]

Hope this helps.
Cordell Lawrence [[email protected]]
Teleios Systems Ltd.

"System.Reflection Activator" <System.Reflection
(e-mail address removed)> wrote in message
**************************************
//Load the Assembly
Assembly a = Assembly.LoadFrom(sAssembly);

//Get Types so we can Identify the Interface.
Type[] mytypes = a.GetTypes();
BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);

//Iterate through the Assembly to find Class with a Public Interface.
//****Each Assembly should only have one Public Entry Point****
//****using this method to expose that method and pass the XML data to it****

foreach (Type t in mytypes)
{
MethodInfo[] mi = t.GetMethods(flags);
Object obj = Activator.CreateInstance(t);

**************************************

I am trying to dynamically call an assembly. The Assembly name is being
pulled from a SharePoint List and in this case it is called
TransformXML.
I
am getting the following error when it gets to the Activator...

ERROR:
**********************************************************
*An unhandled exception of type 'System.MissingMethodException'
occurred
in
* *mscorlib.dll
*
*
*
*Additional information: No parameterless constructor defined for this
object *
**********************************************************



I am also including the code from the class being called.

Transform.dll
**************************************************
public class CreateTransform
{
#region Public Methods

public CreateTransform()
{
}

public CreateTransform(string sXML, Microsoft.SharePoint.SPListItem
_spListItem)
{
//System.Xml.XmlDocument xmlDoc;

Microsoft.SharePoint.SPAttachmentCollection spAttachColl =
this.GetXSLTransform(_spListItem);

this.TransformXML(sXML,spAttachColl);

}

#endregion
 

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

Similar Threads


Top