Denaes Posted July 14, 2006 Posted July 14, 2006 Seems funny, but Typed TableAdapters inherit from the Component Class. But, they offer the functionalty for you to choose another Base Class that also inherits from Component. So, knowing there will always be an Update Method in a Typed TableAdapter, is there a way to call it from the Base Class? Quote
Administrators PlausiblyDamp Posted July 14, 2006 Administrators Posted July 14, 2006 If you have control over the base class then you can either provide a virtual method yourself that can be overrriden in a derived class, or make the base class abstract and provide an abstract method that must then be overrriden in the derived class. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Denaes Posted July 14, 2006 Author Posted July 14, 2006 If you have control over the base class then you can either provide a virtual method yourself that can be overrriden in a derived class' date=' or make the base class abstract and provide an abstract method that must then be overrriden in the derived class.[/quote'] That looks like an awful lot of text to say no, but I don't see you mentioning any way to call a procedure in a child class, so I guess it is. See, the problem here is that you're talking about changing or customizing the child class to allow for this. A Typed DataSet does let you alter the code, though it's dynamically generated and changes would be overrwritten by Visual Studio. The Typed Dataset lets you choose a Base Class, not change the autogenerated code. I'm sure I could rig something up with partial classes, but then I'm back to adding code to every dataset file rather than using OOP techniques to save code/time. Quote
MrPaul Posted July 17, 2006 Posted July 17, 2006 If you've no aversion to black arts there is always reflection: using System.Reflection; //(Untested, but should be ok) private void InvokeUpdate(...) { MethodInfo method; try { method = this.GetType().GetMethod("Update"); //Get the method method.Invoke(this, ...); } catch (Exception e) { //Do something with e, please } } Note that ... is a placeholder for whatever parameters this method takes. Also, if you're going to be calling this method many times, it might be advisable to store the MethodInfo in a class-level variable (though I'm not sure if this is possible in a constructor...). Good luck! :cool: Quote Never trouble another for what you can do for yourself.
Denaes Posted July 17, 2006 Author Posted July 17, 2006 I've always wanted to dabble in the Dark Arts of Reflection. It looks extremely useful. I'll try to hack that to VB later on today and see how that flies :D Quote
Denaes Posted July 18, 2006 Author Posted July 18, 2006 (edited) Okay, got it working flawlessly. MrPaul had me about 90% the way there and on the path, just some hitches I had to work through :D Imports System.Reflection ''' <summary> ''' BaseDataAdapter is inherits from ComponentModel.Component and will be set as BaseClass for all ''' Typed TableAdapters to allow a common code base so generic procedures can be created. ''' </summary> ''' <remarks> ''' Created by: Denaes ''' Created on: 07/19/2006 ''' </remarks> Public Class BaseDataAdapter Inherits System.ComponentModel.Component Implements ITableAdapter Public Sub InvokeUpdate(ByVal Table As DataTable) Implements ITableAdapter.InvokeUpdate Debug.Print("Invoke Update procedure for " & Table.TableName & " begun.") If Table IsNot Nothing Then ' Method to invoke. Dim UpdateMethod As MethodInfo ' Array of Methods parameter signiture types. Dim UpdateTypes As Type() = {Table.GetType} ' Not used, just a dummy value. Dim UpdateModifiers() As ParameterModifier = {New ParameterModifier} ' Array of Parameters to pass into the Method being Invoked. Dim arrUpdateParameters() As Object = {Table} Try ' Assigns the Method matching the given signature to UpdateMethod. UpdateMethod = Me.GetType.GetMethod("Update", UpdateTypes, UpdateModifiers) ' Invokes the UpdateMethod with the the Array of Parameters given. UpdateMethod.Invoke(Me, arrUpdateParameters) Catch ex As Exception ' This is a serious error and needs attention if something here fails. MessageBox.Show("Error with InvokeUpdate: " & vbNewLine & ex.ToString) End Try Else Debug.Print("Invoke Update for " & Table.TableName & " canceled. Table is nothing.") End If End Sub End Class Sorry, it stripped out all the whitespace. Well this shows how to create a TableAdapter Base Class that you can set for your Typed DataAdapters. This lets you pass a ITableAdapter Type into a generic procedure which can call the InvokeUpdate procedure which in turn will Invoke the Update method of the type of table you passed in. Reflection isn't the quickest thing, but so far it's the only way I've found to do this. It's not really impacting my performance since this application is doing updates one at a time... maybe batch deletes at times. Any other batch updates will be performed on the database side as management. I have my theories about how this might be sped up for a large batch of updates, primarily by moving UpdateMethod out of the procedure and only setting its value if it's nothing. That would remove most of the code from the equation on multiple calls. Edited July 19, 2006 by Denaes Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.