By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,728 Members | 1,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,728 IT Pros & Developers. It's quick & easy.

System.Reflection Activator.CreateInstance

P: n/a
**************************************
//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
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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 [cl*******@teleios-systems.com]
Teleios Systems Ltd.

"System.Reflection Activator" <System.Reflection
Ac*******@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...
**************************************
//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

Nov 17 '05 #2

P: n/a
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" wrote:
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 [cl*******@teleios-systems.com]
Teleios Systems Ltd.

"System.Reflection Activator" <System.Reflection
Ac*******@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...
**************************************
//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


Nov 17 '05 #3

P: n/a
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" <br****@discussions.microsoft.com> wrote in message
news:29**********************************@microsof t.com...
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" wrote:
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 [cl*******@teleios-systems.com]
Teleios Systems Ltd.

"System.Reflection Activator" <System.Reflection
Ac*******@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...
**************************************
//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


Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.