Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create instance of an object using reflection

Status
Not open for further replies.

desi2002

Programmer
Jan 4, 2002
24
IN
I am not able to create instance of an object using reflection (late binding)
The code is as follows.

Assembly myAssembly = Assembly.LoadFrom("C:\\dev20\\dll\\pwyUserSVCSLib.dll");
Type Atype = myAssembly.GetTypes()[0];
ObjectHandle objass = Activator.CreateInstance(myAssembly.FullName,Atype.FullName);
object[] args = {"qrg3sf55ehabzgqqxapk1p45","136304FE-646E-4483-A453-368981627C47"};
object result;
object objtarget = (object)objass.Unwrap();
BindingFlags flags = (BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance);
result = Atype.InvokeMember("Logoff",flags,null,objtarget,args);
Console.WriteLine("The result is '{0}'",result);
}

I get an error Invalid target type.

what's wrong with this code? Pl suggest.

Thanx
 
Type must be an a array.
Type[] Atype = myAssembly.GetTypes();
 
I could solve this.
To get a name of the class, I used
Type myType = Type.GetTypeFromProgID(Atype.FullName.ToString());

 
The GetTypeFromProgID method is used when you have COM ProgID, and want to call it via COM Interop. Won't work for a pure .net assembly, AFAIK.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Hello there.
This looks the right place to ask my question. ...I have a .NET COM dll with 2 classes defined as follows.

namespace LoyaltyHandler
{
[ComVisible(true), ClassInterface(ClassInterfaceType.AutoDual), GuidAttribute("F3B646DE-CF6C-4170-93EB-1F63351CBC90")]
public class Transaction
{
/// <remarks/>
public int SiteID;

/// <remarks/>
public int TransactionHeaderID;

/// <remarks/>
public System.DateTime BusinessDate;

/// <remarks/>
public System.Double GrossValue;

/// <remarks/>
public TransactionPayment[] Payment;
}

[ComVisible(true), ClassInterface(ClassInterfaceType.AutoDual), GuidAttribute("AC2BF33C-B697-42b6-A47F-B8E9950800BB")]
public class TransactionPayment
{

/// <remarks/>
public string PaymentMethod;

/// <remarks/>
public System.Double GrossValue;
}
}

I then compiled the classes into a LoyaltyHandler.dll and ran the following at the command prompt and executed the RegFile.reg file produced:
RegAsm.exe "LoyaltyHandler.dll" /regfile:"RegFile.reg" /codebase
This registers the component correctly.

However, when I create a VB5 application and reference this DLL. I am unable to assign an array of TransactionPayment objects to Transaction.Payment.
Dim oLoyaltyHandler As New LoyaltyHandler_Transaction
Dim ps() As New LoyaltyHandler_TransactionPayment
ReDim ps(2)

Dim p1 As New LoyaltyHandler_TransactionPayment
p1.PaymentMethod = "loyalty card"
p1.GrossValue = 50
Set ps(0) = p1

Dim p2 As New LoyaltyHandler_TransactionPayment
p2.PaymentMethod = "credit card"
p2.GrossValue = 50
Set ps(1) = p2

Set t.Payment = ps 'Function or Interface marked as restricted, or the function uses an automation type not supported in Visual Basic.

In the object browser. The definition of LoyaltyHandler_Transaction.Payment is as follows:
Property Payment As () As LoyaltyHandler_TransactionPayment
Member of LoyaltyHandler.LoyaltyHandler_Transaction

In OLEView I get the following:
[id(0x6002000e), propget]
SAFEARRAY(_LoyaltyHandler_TransactionPayment*) Payment();
[id(0x6002000e), propput]
void Payment([in] SAFEARRAY(_LoyaltyHandler_TransactionPayment*) rhs);

Does anyone know how I can resolve this issue?
Thanx in advance for any help.
 
For Richard:
>>Passing Arrays to a DLL Procedure

If the DLL procedure was written especially for Automation,
then you may be able to pass an array to the procedure the same way you pass an array to a Visual Basic procedure: with empty parentheses.
Because Visual Basic uses Automation data types, including SAFEARRAYs, the DLL must be written to accommodate Automation for it to accept Visual Basic array arguments.
For further information, consult the documentation for the specific DLL.

If the DLL procedure doesn't accept Automation SAFEARRAYs directly, you can still pass an entire array if it is a numeric array.
You pass an entire numeric array by passing the first element of the array by reference.
This works because numeric array data is always laid out sequentially in memory. If you pass the first element of an array to a DLL procedure, that DLL then has access to all of the array's elements.
-obislavu-


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top