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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB.NET to C# Conversion - Late binding

Status
Not open for further replies.

JoeAtWork

Programmer
Jul 31, 2005
2,285
0
0
CA
I had one issue with VB.NET to C# conversion that I couldn’t solve. There’s a method passing in a parameter like this:

Code:
public FormErrorCode SetFormProperties(object oFormPropCol)

later on it does something like this:

Code:
pFormProps.PropertiesUpdate(0, 0, pForm.ID, [COLOR=red]oFormPropCol(1).Name[/color], 0, oFormPropCol(1).Value, "", 0, lPropID);

So, it’s using late binding. I solved late binding issues elsewhere by using MyObject.GetType().InvokeMethod, but here I don’t know what to send as the “method name” argument.


Joe Schwarz
Custom Software Developer
 
you cannot simply pass a method name as a variable. you would need to use a delegate.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
The code above was the exact translation from VB.NET to C#. VB.NET was relying on late-binding.

This is my code so far using reflection to retrieve property values from the late-bound object. This is not yet tested, and my understanding of reflection is weak at the moment. I would appreciate any review of the code.

Code:
public void MyMethod(object oFormPropCol)
{
  int curProp = 1;
  int propCount = 0;
  object[] objParams = new object[1];
  object[] methodParams = new object[] { };
  string propertyName = "";
  string propertyValue = "";

  // We are assuming the object has a "Count" property
  propCount = (long)oFormPropCol.GetType().InvokeMember("Count", System.Reflection.BindingFlags.InvokeMethod, null, oFormPropCol, methodParams);

  for (curProp = 1; (curProp <= propCount); curProp++)
  {
    objParams[0] = curProp;
    propertyName = GetPropertyOfDefaultMember(oFormPropCol, objParams, "Name");
    propertyValue = GetPropertyOfDefaultMember(oFormPropCol, objParams, "Value");

    SomethingThatUsesTheseValues(curProp, propertyName, propertyValue);
  }
}

/// <summary>
/// Retrieves the value of a property of the default member of an object
/// </summary>
/// <param name="UnkownObject">The object whose default member we want to access</param>
/// <param name="methodParams">Object array of any parameters that must be supplied
/// to the default method</param>
/// <param name="PropertyName">Name of the property we wish to get the value of</param>
/// <returns>A string containing the value of the property</returns>
private string GetPropertyOfDefaultMember(object UnkownObject, object[] methodParams, string PropertyName)
{
    object defaultMember = null;
    string propertyValue = "";
    object[] objectParams = new object[] { };

    try
    {
        // Reference the default member
        defaultMember = UnkownObject.GetType().InvokeMember("", BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod,
        Type.DefaultBinder, UnkownObject, methodParams);

        // Now get the property value from it
        propertyValue = defaultMember.GetType().InvokeMember(PropertyName, BindingFlags.InvokeMethod, null, defaultMember, objectParams).ToString();
    }
    catch (Exception e)
    {
        System.IO.File.AppendAllText((strLogPath2 + "\\MyLogLog.txt"), (e.Message + "  " + LogTime()));
    }

    return propertyValue;
}


Joe Schwarz
Custom Software Developer
 
this is making it harder than it needs to be.
1. use strong types (actual interface/class) not object.
2. remove all the reflection and use the actual properties/members to complete the work.
3. this appears to be a "generic" logging method. use a logging framework like log4net, nlog, or ent. lib. logging instead of rolling your own.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Yes, I know it's crap, it's not my code! Had it been mine I would have done all you suggest. But this version has already been released, so I have to support the current interface.

That means the caller is allowed to pass in any generic object. I have no other choice than to support late binding.

We are planning to use Log4Net as well, but the priority for now is to support the current version.


Joe Schwarz
Custom Software Developer
 
if you're programming to an interface, than the implementation does not matter. pass the object to a logging library and let the library take over. use your logging object as an adapter for log4net.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top