VB.NET - How to get Assembly from PropertyInfo? - vb.net

I need to be able to dynamically instantiate an object using reflection. I have a PropertyInfo at this point and need to get the Assembly of this object before I can call the CreateInstance method.
The code looks like this:
Dim subObjectPropInfo As PropertyInfo = GetPropertyInfo(baseObject, "Name")
Dim asm As Assembly = ?????
Dim subObjType As Type = asm.GetType(subObjectPropInfo.PropertyType.FullName)
Dim subObject As Object = Activator.CreateInstance(subObjType)
The question is: How can I get the assembly for the object I need to instantiate from a PropertyInfoobject ? Is it possible ? If not, what is the way to do it ?
I use to have always the same assembly so the way to do it was not really dynamic. Now that objects come from multiple assemblies, it cannot be done the way it used to be.

Try using the PropertyType.Assembly properties from the PropertyInfo object.
subObjectPropInfo.PropertyType.Assembly

Related

ListBox.SelectedObjectCollection is not queryable

I'm new to programming, and I'm making a new project in visual studio 2017rc. There I want to make the use of a list box. I want to select the items in list box and make some actions.
So I wrote the following code:
Dim SelectedItems = (From i In ListBox1.SelectedItems).ToList
For Each selecteditem In SelectedItems
Peca = selecteditem
Call CATIA_Windows_app.Save()
Next
But this is returning an error:
I had used this code in other application and it works.
Have I forgot something?
Thanks
You need an object that implements IEnumerable(Of T) for a LINQ query and that type only implements IEnumerable. The way to get the former from the latter is to call the Cast method. For instance, if your ListBox contains String objects you can do this:
From item In myListBox.SelectedItems.Cast(Of String)()

Copy VB.NET list object to another list object

I am having a problem when setting VB.NET list object to another. In the example below
I create an instance on ReadLocations and than create an object of ReadLocation where then I loop through ReadLocations and set ReadLocation1 equal to rl.
What happens is that if I then go and change ReadLocation1 to something else (assdfhsd) it will also change the ReadLocations index. I am really confused why it would be doing that unless it is "=" sign means referencing instead of actually setting the value. Please help as I am a C# developer but the program I am modifying is in VB.NET.
Dim ReadLocations As New List(Of Model.ReadLocation)
Dim rl1 As New Model.ReadLocation
rl1.LL = "a"
Dim rl2 As New Model.ReadLocation
rl2.LL = "b"
ReadLocations.Add(rl1)
ReadLocations.Add(rl2)
Dim ReadLocation11 As New Model.ReadLocation
For Each rl As Model.ReadLocation In ReadLocations
ReadLocation11 = rl
Next
If ReadLocation is a reference type (a Class), then all variables set to instances of objects of that class will always be references. The = operator only ever sets a new reference to an object when it is operating on reference types. It will never make a clone of the object (unless it is a value type). The same is true in C#. The only way to do what you want to do, would be to clone the objects when you add them to the second list. Unfortunately, .NET doesn't provide a simple automatic method for cloning any object. The standard way to do this would be to implement the ICloneable interface in your ReadLocation class. Then you could clone it by calling the clone method:
ReadLocation1 = CType(rl.Clone(), ReadLocation)
However, inside that clone method, you will need to create a new instance of ReadLocation and manually set all of its properties and then return it. For example:
Public Class ReadLocation
Implements ICloneable
Public Function Clone() As Object Implements ICloneable.Clone
Dim clone As New ReadLocation()
clone.Property1 = Me.Property1
clone.Property2 = Me.Property2
Return clone
End Function

Autovivified properties?

suppose I declare a class like this:
Class tst
Public Props As New Dictionary(Of String, MyProp)
End Class
and added properties something along these lines:
Dim t As New tst
t.Props.Add("Source", new MyProp(3))
but now want to access it like this:
t.Source
how can I create a getter without knowing the name of the getter?
Ok, if you insist on "auto-vivifying", the only way I know of to do something like that is to generate the code as a string, and then compile it at runtime using the classes in the System.CodeDom.Compiler namespace. I've only ever used it to generate complete classes from scratch, so I don't know if you could even get it to work for what need to add properties to an already existing class, but perhaps you could if you compiled extension methods at runtime.
The .NET framework includes multiple implementations of the CodeDomeProvider class, one for each language. You will most likely be interested in the Microsoft.VisualBasic.VBCodeProvider class.
First, you'll need to create a CompilerParameters object. You'll want to fill its ReferencedAssemblies collection property with a list of all the libraries your generated code will need to reference. Set the GenerateExecutable property to False. Set GenerateInMemory to True.
Next, you'll need to create a string with the source code you want to compile. Then, call CompileAssemblyFromSource, passing it the CompilerParameters object and the string of source code.
The CompileAssemblyFromSource method will return a CompilerResults object. The Errors collection contains a list of compile errors, if there are any, and the CompiledAssembly property will be a reference to your compiled library (as an Assembly object). To create an instance of your dynamically compiled class, call the CompiledAssembly.CreateInstance method.
If you're just generating a small amount of code, it's pretty quick to compile it. But if it's a lot of code, you may notice an impact on performance.
Here's a simple example of how to generate a dynamic class containing a single dynamic property:
Option Strict Off
Imports System.CodeDom.Compiler
Imports Microsoft.VisualBasic
Imports System.Text
Public Class Form3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim code As StringBuilder = New StringBuilder()
code.AppendLine("Namespace MyDynamicNamespace")
code.AppendLine(" Public Class MyDynamicClass")
code.AppendLine(" Public ReadOnly Property WelcomeMessage() As String")
code.AppendLine(" Get")
code.AppendLine(" Return ""Hello World""")
code.AppendLine(" End Get")
code.AppendLine(" End Property")
code.AppendLine(" End Class")
code.AppendLine("End Namespace")
Dim myDynamicObject As Object = generateObject(code.ToString(), "MyDynamicNamespace.MyDynamicClass")
MessageBox.Show(myDynamicObject.WelcomeMessage)
End Sub
Private Function generateObject(ByVal code As String, ByVal typeName As String) As Object
Dim parameters As CompilerParameters = New CompilerParameters()
parameters.ReferencedAssemblies.Add("System.dll")
parameters.GenerateInMemory = True
parameters.GenerateExecutable = False
Dim provider As VBCodeProvider = New VBCodeProvider()
Dim results As CompilerResults = provider.CompileAssemblyFromSource(parameters, code)
If results.Errors.HasErrors Then
Throw New Exception("Failed to compile dynamic class")
End If
Return results.CompiledAssembly.CreateInstance(typeName)
End Function
End Class
Note, I never use Option Strict Off, but for the sake of simplicity in this example, I turned it off so I could simply call myDynamicObject.WelcomeMessage without writing all the reflection code myself.
Calling methods on objects using reflection can be painful and dangerous. Therefore, it can be helpful to provide a base class or interface in a shared assembly which is referenced by both the generated assembly, and the fixed assembly which calls the generated assembly. That way, you can use the dynamically generated objects through a strongly typed interface.
I figured based on your question that you were just more used to dynamic languages like JavaScript, so you were just thinking of a solution using the wrong mindset, not that you really needed to or even should be doing it this way. But, it is definitely useful in some situations to know how to do this in .NET. It's definitely not something you want to be doing on a regular basis, but, if you need to support custom scripts to perform complex validation or data transformations, something like this can be very useful.

How to load a class into the current instance within Sub New

Long term lurker, first time poster here.
I have written a class to model an object in vb.net, using vs2008 and framework 2.0. I am serializing the class to an XML file for persistent storage. I do this with a method in the class like this:
Public Sub SaveAs(ByVal filename As String)
Dim writer As New Xml.Serialization.XmlSerializer(GetType(MyNamespace.MyClass))
Dim file As New System.IO.StreamWriter(filename)
writer.Serialize(file, Me)
file.Close()
End Sub
I now want to do a similar thing but reading the class from file to the current instance, like this:
Public Sub New(ByVal filename As String)
Dim reader = New Xml.Serialization.XmlSerializer(GetType(MyNamespace.MyClass))
Dim file = New System.IO.StreamReader(FullPath)
Me = CType(reader.Deserialize(file), MyNamespace.MyClass)
End Sub
However, I cannot assign anything to “Me”. I’ve tried creating a temporary object to hold the file contents then copying each property and field over to the current instance. I iterated over the properties (using Reflection), but this soon gets messy, dealing with ReadOnly collection properties, for example. If I just copy each property manually I will have to remember to modify the procedure whenever I add a property in the future, so that sounds like a recipe for disaster.
I know that I could just use a separate function outside the class but many built-in .NET classes can instantiate themselves from file e.g. Dim bmp As New Bitmap(filename As String) and this seems very intuitive to me.
So can anyone suggest how to load a class into the current instance in the Sub New procedure? Many thanks in advance for any advice.
I'd put a shared load function on the class, that returned the newly de-serialised object.
e.g.
Public Class MyClass
...
Public shared Function Load(ByVal filename As String) as MyClass
Dim reader = New Xml.Serialization.XmlSerializer(GetType(MyNamespace.MyClass))
Dim file = New System.IO.StreamReader(FullPath)
Return CType(reader.Deserialize(file), MyNamespace.MyClass)
End Sub
End Class
...
Dim mine as MyClass = MyClass.Load("MyObject.Xml");
Hope this helps
Alternatively,
Encapsulate the data of your class in an inner, private class.
The properties on your outer visible class delegate to the inner class.
Then Serialising and De-serialising happens on the inner class, you can then have a ctor that takes the file name, de-serialises the inner hidden object, and assigns it to the classes data store.
The "New" method in VB.Net is a constructor for the class. You can't call it for an existing instance, as the whole purpose of the method is to create new instances; it's just not how the language works. Try naming the method something like "ReadFrom" or "LoadFrom" instead.
Additionally, given those methods, I would try to implement them using a Factory Pattern. The ReadFrom method would be marked Shared and return the new instance. I would also make the method more generic. My main ReadFrom() method would accept an open textreader or xmlreader or even just a stream, rather than a file name. I would then have overloads that converts a file name into a stream for reading and calls the main method.
Of course, that assumes I use that pattern in the first place. .Net already has great support for xml serialization built into the platform. Look into the System.Xml.Serialization.XmlSerializer class and associated features.

vb.net - creating array/list from loaded interfaces

I'm having trouble creating a global array that i can use in other functions.
I have this code right under "Public Class myClass":
Dim LoadedPlugins As Array
Then in a function I have this:
Dim PluginList As String() = Directory.GetFiles(appDir, "*.dll")
For Each Plugin As String In PluginList
Dim Asm As Assembly
Dim SysTypes As System.Type
Asm = Assembly.LoadFrom(Plugin)
SysTypes = Asm.GetType(Asm.GetName.Name + ".frmMain")
Dim IsForm As Boolean = GetType(Form).IsAssignableFrom(SysTypes)
If IsForm Then
Dim tmpPlugin As PluginAPI = CType(Activator.CreateInstance(SysTypes), PluginAPI)
LoadedPlugins(count) = tmpPlugin
In the Interface file I have:
Public Interface PluginAPI
Function PluginTitle() As String
Function PluginVersion() As String
Function CustomFunction() As Boolean
End Interface
Now obviously that doesn't work, how can I add the tmpPlugin to an array or list so I can use it in other functions?
The main thing I need to be able to do is loop through all the loaded plugins and execute the CustomFunction in a separate function than the one that loads the plugins listed above.
Can anyone help me?
Use this for your dim:
Public Shared LoadedPlugins As Array
However I notice a few things
Some tips to make writing programs faster (in the end, but more thinking for design):
use Option Strict.
give LoadedPlugins a type (PluginAPI)
use a generic list instead of array
don't use globals. Figure out a way to give this info to the classes that need it
this is a bit of an art. But don't give up - 5 programs later you will have it down!