|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Property of List of ClassesI asked this question before but got no responses. Let me rephrase the
question. I have a class that as a property defined as a list of classes. Public Class ClassA Public Property Prop1 as List(of ClassB) In a code segment, I can add an instance of ClassB to the Prop1 property of an instance of ClassA with the statement: myClassA.Prop1.Add myClassB Can I add an instance of ClassB to the Prop1 List using the ..InvokeMember method of the Type class? Try:
Public Class ClassB End Class Public Class ClassA Public Prop1 As New List(Of ClassB) End Class Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim objA As New ClassA Dim objArray(0) As ClassB objA.Prop1.Add(New ClassB) objArray(0) = New ClassB objA.Prop1.GetType.InvokeMember("Add", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.InvokeMethod, Nothing, objA.Prop1, objArray) MessageBox.Show(objA.Prop1.Count.ToString) ' Result: 2 End Sub -- Show quoteHide quoteBest regards, Carlos J. Quintero MZ-Tools: Productivity add-ins for Visual Studio You can code, design and document much faster: http://www.mztools.com <za***@construction-imaging.com> escribió en el mensaje news:1140104688.906608.16700@g47g2000cwa.googlegroups.com... >I asked this question before but got no responses. Let me rephrase the > question. I have a class that as a property defined as a list of > classes. > > Public Class ClassA > > Public Property Prop1 as List(of ClassB) > > In a code segment, I can add an instance of ClassB to the Prop1 > property of an instance of ClassA with the statement: > > myClassA.Prop1.Add myClassB > > Can I add an instance of ClassB to the Prop1 List using the > .InvokeMember method of the Type class? > Carlos J. Quintero [VB MVP] wrote:
> objArray(0) objA.Prop1.GetType.InvokeMember("Add", Reflection.BindingFlags.Instance Oh, man, I was thinking the only way to set a property value in> Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.InvokeMethod, > Nothing, objA.Prop1, objArray) InvokeMethod was to use the bindings flag of SetProperty. I never thought of Invoking the Add method!! Very elegant. I will try your suggestion!
How is it possible ...
Edanmo Shell Extension: How to Install it? Unmanaged DLL Callback - Program Unexpectedly Quits Carriage Return and Line Feed Syntax need help reading contents of a file into an array...SKYPE ME. Transparent splash screen tab function in a multiline text box Treeview, Transparent Label TripleDES class - Suggestions Requested. |
|||||||||||||||||||||||