473,378 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

System.Reflection Activator.CreateInstance

**************************************
//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
3 17038
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
1
by: John Jenkins | last post by:
Hi, I have a fairly simeple question. What are the differences between Assembly.CreateInstance and System.Activator.CreateInstance? I had read that one maps to the other, however when I use...
3
by: Bisbal | last post by:
Hi, I'd like to know if it is possible to create a form type from a string. My case: I have a DataTable with forms names (i.e frm_001_UserInfo.cs), and I'd like to create an instance of that...
2
by: Gujju | last post by:
Hi all, I m new to this class.. With System.Reflection... u can create run time objects for a specified class in a assembly... Now if once the Assembly is loaded in the memory then does...
2
by: Doug | last post by:
I've installed ASP.NET onto our web server (Win2k/unsure of IIS version) this morning. I created a simple ASPX page in Notepad that writes out an asp:Label control to the page and dynamically...
2
by: Jason | last post by:
Hello, I"m trying to invoke a method using reflection on a DLL that is already within a project. However, I"m having a few problems and didn't know if anyone has any bright ideas. Please note,...
1
by: Johnny R | last post by:
Hello, I'm loading a Class from Assemly DLL using Activator.CreateInstance. That loaded Class is executed in a worker Thread with no loop. What actually happends when class is loaded using...
5
by: Nightfall | last post by:
Dear friends, consider the following scenario, in Visual Studio 2005 and C# - I create a ASP.NET web service ("server") with a class MyClass and a WebMethod SomeMethod() - I create a client...
4
by: Rob Blackmore | last post by:
Hi, Can anyone advise how to call a private constructor using reflection? I currently get the above error which is rectified by changing the New() to Public from Friend but I ideally wish to...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.