Lambda Expression to Loop Through Class Properties - vb.net

If let's say I have the following classes:
Public Class Vehicle
Sub New()
Car = New Car()
VehicleName = String.Empty
End Sub
Public Property Car As Car
<Mask()>
Public Property VehicleName As String
End Class
Public Class MaskAttribute
Inherits Attribute
Public Property Masking As String
End Class
<Serializable()>
Public Class Car
Sub New()
CarName = String.Empty
End Sub
<Mask()>
Public Property CarName As String
End Class
In the above sample codes, there is a custom attribute name Mask.
Given, there is an object Dim v As new Vehicle()
How to get all the properties of that object which have Mask custom attributes?
So in this case, the expected looping through it are Properties: CarName, and VehicleName as they both have mask attribute
I understand if I use reflection the performance would be slower rather than using lambda expression. Please correct me if I am wrong.
Any idea to achieve that objective using lambda expression?
Thanks!

Use the following code to get a list with all property descriptors with the Mask attribute of the v object (the name you used in your example):
Dim props As List(Of PropertyDescriptor) = (
From C As PropertyDescriptor In TypeDescriptor.GetProperties(v.GetType)
Where C.Attributes.OfType(Of MaskAttribute)().Count > 0
Select C
).ToList
You need to import System.ComponentModel
Retrieving the property value
If you need the value of property, you can use the following code (Access property using its name in vb.net):
Public Function GetPropertyValue(ByVal obj As Object, ByVal PropName As String) As Object
Dim objType As Type = obj.GetType()
Dim pInfo As System.Reflection.PropertyInfo = objType.GetProperty(PropName)
Dim PropValue As Object = pInfo.GetValue(obj, Reflection.BindingFlags.GetProperty, Nothing, Nothing, Nothing)
Return PropValue
End Function
Observe that in our example, the name of property is given by the property Name of each PropertyDescriptor in the list props.
UPDATE
This would not work in your example because you have an object of car type inside other object of vehicle type, and I was not considering the inner object.
The way I found to work with this is using recursion:
Sub GetPropertiesWithMaskAttribute(Obj As Object, ByRef props As List(Of PropertyDescriptor))
Dim props1 As List(Of PropertyDescriptor) = (From C As PropertyDescriptor In TypeDescriptor.GetProperties(Obj) Select C).ToList
For Each prop In props1
If prop.Attributes.OfType(Of MaskAttribute)().Count > 0 Then
props.Add(prop)
Else
If prop.ComponentType.IsClass Then
GetPropertiesWithMaskAttribute(GetPropertyValue(Obj, prop.Name), props)
End If
End If
Next
End Sub
Calling like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim props As New List(Of PropertyDescriptor)
GetPropertiesWithMaskAttribute(v, props)
End Sub
Then the props list will contains all properties with MaskAtribute attribute. Observe that I used the sub GetPropertyValue declared before.

Related

How to access object in multidimensional array in Visual Basic

In Visual Basic, I am using a List to store an array of objects. However, I haven't figured out how to access a particular object once I have added it to my list.
I am using Microsoft Visual Studio
Private Results As New List(Of Object)
Dim Results As Object()
Dim CaseStatus as String = "Closed"
...
Results.Add(New Object() {CaseStatus, FlagsStr, OBTotal, OBPaid})
MessageBox.Show(Result(0).ToString)
Displays: System.Object[]
I have also tried MessageBox.Show(Results(0,0).ToString), but don't get the expected results.
When you can .ToString on an object you get the fully qualified name of the object unless the class overrides .ToString. Creating a class and a List (Of T) gives you easy access to any of the properties of the class.
Public Class ProjectCase
Public Property CaseStatus As String
Public Property FlagsString As String
Public Property OBTotal As Decimal
Public Property OBPaid As Decimal
Public Sub New()
'include a default constructor
End Sub
Public Sub New(cs As String, fs As String, total As Decimal, paid As Decimal)
CaseStatus = cs
FlagsString = fs
OBTotal = total
OBPaid = paid
End Sub
Public Overrides Function ToString() As String
Return $"{CaseStatus}, {FlagsString}, Total = {OBTotal}, Paid = {OBPaid}"
End Function
End Class
Private Results As New List(Of ProjectCase)
Private Sub OPCode()
Results.Add(New ProjectCase("Closed", "Some string", 74.36D, 22D))
MessageBox.Show(Results(0).ToString)
MessageBox.Show(Results(0).CaseStatus) 'or any property of your class
End Sub

Instantiate sub classes recursively without calling constructor?

Assume the following example class which mimics the type of class generated from an XSD file:
Public Class MyClass
Public Class MyInnerClass1
Public Class MyInnerInnerClass1
Public Property MyProp1 as string
Public Property MyProp2 as string
...
End Class
...
Public Property MyInnerInnerClassProp1 as MyInnerInnerClass1
End Class
Public property MyInnerClassProp1 as MyInnerClass1
Public property MyInnerClassProp2 as MyInnerClass2
...
End Class
Notice that there are no constructors. The level of inner classes, in this particular case, can go 5 levels deep, possibly circularly, before hitting a base property such as Property MyProp1 as string.
How can I recursively iterate through ALL of the public writable properties and initialize them as new instances of that object type without constructors?
For example, here is my current code which only goes one level deep at the moment?
Private Shared Sub InitProperties(obj As Object)
For Each prop As Object In obj.[GetType]().GetProperties(BindingFlags.[Public] Or BindingFlags.Instance).Where(Function(p) p.CanWrite)
Dim type__1 = prop.PropertyType
Dim constr = type__1.GetConstructor(Type.EmptyTypes)
'find paramless const
If type__1.IsClass Then
Dim propertyInstance = DirectCast(FormatterServices.GetUninitializedObject(type__1.GetType()), Object)
'Dim propInst = Activator.CreateInstance(type__1)
'prop.SetValue(obj, propInst, Nothing)
InitProperties(propertyInstance)
End If
Next
End Sub
I did some small edits to your code to get it to work on the example class you provided. (Although I did change the string properties to Integer to avoid one error.) I also added an argument for limiting the number of recursive calls, and a check that a property is equal to nothing before initializing it. (This check will only make a difference if you have circular references between static classes.)
Private Shared Sub InitProperties(obj As Object, Optional ByVal depth As Integer = 5)
For Each prop As PropertyInfo In obj.GetType().GetProperties(BindingFlags.Public Or BindingFlags.Instance).Where(Function(p) p.CanWrite)
Dim type__1 As Type = prop.PropertyType
If type__1.IsClass And IsNothing(prop.GetValue(obj, Nothing)) And depth > 0 Then
Dim propertyInstance As Object = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(type__1)
prop.SetValue(obj, propertyInstance, Nothing)
InitProperties(propertyInstance, depth - 1)
End If
Next
End Sub

Bind a detail report to a class model with properties stored in a Dictionary (VB.NET, Devexpress)

I'm trying the example from Devexpress
Dim binding As New XRBinding("Text", dsProducts1, "Products.UnitPrice")
But my models does not have their properties explicitly written in their class. It would take a method GetProperty("column_name_here") to get it's data. I'm wondering if the 3rd parameter of XRBinding can be a method? Like:
Dim binding As New XRBinding("Text", dsProducts1, product.GetProperty("name"))
Additional Info:
All of my model classes extends this Dao class which is responsible in getting data in the database. The Dao class have a protected variable as a Dictionary(Of String, Object) to store the values (key = column name, value = column row value) from the database.
Now when I want to get something in the database, I only call
Dim user As New User // this class extends the Dao class
Dim userId = user.GetProperty("id") // Method to get the value from Dictionary, first parameter is the Dictionary key or column name from the DB
I made this so that I wont have to create every model class and set the properties of that class, as it is kinda cumbersome.
It seems that there are no way to bind to some method. I suggest you to take a look at ExpandoObject dynamic class. The members of this class can be added at runtime. This class implements IDictionary(Of String, Object) interface which you can use to generate properties from your Dictionary(Of String, Object).
Here is example:
Example of base Dao class implementation with protected Dictionary(Of String, Object) property:
Public Class Dao
Private _values As Dictionary(Of String, Object)
Public Sub New()
_values = New Dictionary(Of String, Object)
End Sub
Public Overridable Sub Fill(index As Integer)
_values.Clear()
_values.Add("ID", index)
_values.Add("Product", "Banana " & index)
_values.Add("Price", 123.45 + index)
End Sub
Protected ReadOnly Property Values As Dictionary(Of String, Object)
Get
Return _values
End Get
End Property
End Class
Example of Dao class descendant with DynamicValues property which returns ExpandoObject based on Dictionary(Of String, Object) (you must omit the type of property):
Public Class DynamicDao
Inherits Dao
Private _dynamicValues As ExpandoObject
Public Overrides Sub Fill(index As Integer)
MyBase.Fill(index)
_dynamicValues = New ExpandoObject()
Dim keyValues = DirectCast(_dynamicValues, IDictionary(Of String, Object))
For Each pair In Values
keyValues.Add(New KeyValuePair(Of String, Object)(pair.Key, pair.Value))
Next
End Sub
Public ReadOnly Property DynamicValues ' <= There is no type. In hint is displayed «As Object».
Get
Return _dynamicValues
End Get
End Property
End Class
Usage of DynamicDao class in XtraReport:
Dim list = New List(Of DynamicDao)
For index% = 0 To 9
Dim dao = New DynamicDao()
dao.Fill(index%)
list.Add(dao)
Next
Dim labelID = New XRLabel()
labelID.DataBindings.Add(New XRBinding("Text", Nothing, "DynamicValues.ID"))
Dim labelProduct = New XRLabel()
labelProduct.DataBindings.Add(New XRBinding("Text", Nothing, "DynamicValues.Product"))
labelProduct.LeftF = 50
Dim labelPrice = New XRLabel()
labelPrice.DataBindings.Add(New XRBinding("Text", Nothing, "DynamicValues.Price"))
labelPrice.LeftF = 150
Dim detail = New DetailBand()
detail.Controls.Add(labelID)
detail.Controls.Add(labelProduct)
detail.Controls.Add(labelPrice)
Dim report = New XtraReport()
report.Bands.Add(detail)
report.DataSource = list
report.ShowRibbonPreview()

create a list from a object passed through

Is it possible to create a list ListofOb from the datatype of OB without doing an IF statement to check what object type it is?
Class CustomCLass
Public ListofOb As List(Of Object)
Public Sub New(OB As Object)
ListofOb = New List(Of gettype(OB))
End Sub
End Class

VB.NET CType: How do I use CType to change an object variable "obj" to my custom class that I reference using a string variable like obj.GetType.Name?

The code below works for the class that I hard coded "XCCustomers" in my RetrieveIDandName method where I use CType. However, I would like to be able to pass in various classes and property names to get the integer and string LIST returned. For example, in my code below, I would like to also pass in "XCEmployees" to my RetrieveIDandName method. I feel so close... I was hoping someone knew how to use CType where I can pass in the class name as a string variable.
Note, all the other examples I have seen and tried fail because we are using Option Strict On which disallows late binding. That is why I need to use CType.
I also studied the "Activator.CreateInstance" code examples to try to get the class reference instance by string name but I was unable to get CType to work with that.
When I use obj.GetType.Name or obj.GetType.FullName in place of the "XCCustomers" in CType(obj, XCCustomers)(i)
I get the error "Type 'obj.GetType.Name' is not defined" or "Type 'obj.GetType.FullName' is not defined"
Thanks for your help.
Rick
'+++++++++++++++++++++++++++++++
Imports DataLaasXC.Business
Imports DataLaasXC.Utilities
Public Class ucCustomerList
'Here is the calling method:
Public Sub CallingSub()
Dim customerList As New XCCustomers()
Dim customerIdAndName As New List(Of XCCustomer) = RetrieveIDandName(customerList, "CustomerId", " CustomerName")
'This code below fails because I had to hard code “XCCustomer” in the “Dim item...” section of my RetrieveEmployeesIDandName method.
Dim employeeList As New XCEmployees()
Dim employeeIdAndName As New List(Of XCEmployee) = RetrieveIDandName(employeeList, "EmployeeId", " EmployeeName")
'doing stuff here...
End Sub
'Here is the method where I would like to use the class name string when I use CType:
Private Function RetrieveIDandName(ByVal obj As Object, ByVal idPropName As String, ByVal namePropName As String) As List(Of IntStringPair)
Dim selectedItems As List(Of IntStringPair) = New List(Of IntStringPair)
Dim fullyQualifiedClassName As String = obj.GetType.FullName
Dim count As Integer = CInt(obj.GetType().GetProperty("Count").GetValue(obj, Nothing))
If (count > 0) Then
For i As Integer = 0 To count - 1
'Rather than hard coding “XCCustomer” below, I want to use something like “obj.GetType.Name”???
Dim Item As IntStringPair = New IntStringPair(CInt(CType(obj, XCCustomers)(i).GetType().GetProperty("CustomerId").GetValue(CType(obj, XCCustomers)(i), Nothing)), _
CStr(CType(obj, XCCustomers)(i).GetType().GetProperty("CustomerName").GetValue(CType(obj, XCCustomers)(i), Nothing)))
selectedItems.Add(Item)
Next
End If
Return selectedItems
End Function
End Class
'+++++++++++++++++++++++++++++++
' Below are the supporting classes if you need to see what else is happening:
Namespace DataLaasXC.Utilities
Public Class IntStringPair
Public Sub New(ByVal _Key As Integer, ByVal _Value As String)
Value = _Value
Key = _Key
End Sub
Public Property Value As String
Public Property Key As Integer
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCCustomer
Public Property CustomerId As Integer
Public Property CustomerName As String
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCCustomers
Inherits List(Of XCCustomer)
Public Sub New()
PopulateCustomersFromDatabase()
End Sub
Public Sub New(ByVal GetEmpty As Boolean)
End Sub
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCEmployee
Public Property EmployeeId As Integer
Public Property EmployeeName As String
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCEmployees
Inherits List(Of XCEmployee)
Public Sub New()
PopulateEmployeesFromDatabase()
End Sub
Public Sub New(ByVal GetEmpty As Boolean)
End Sub
End Class
End Namespace
From MSDN
CType(expression, typename)
. . .
typename : Any expression that is legal
within an As clause in a Dim
statement, that is, the name of any
data type, object, structure, class,
or interface.
This is basically saying you can't use CType dynamically, just statically. i.e. At the point where the code is compiled the compiler needs to know what typename is going to be.
You can't change this at runtime.
Hope this helps.
Since List(Of T) implements the non-generic IList interface, you could change your function declaration to:
Private Function RetrieveIDandName(ByVal obj As System.Collections.IList, ByVal idPropName As String, ByVal namePropName As String) As List(Of IntStringPair)
And then your troublesome line would become (with also using the property name parameters):
Dim Item As IntStringPair = New IntStringPair(CInt(obj(i).GetType().GetProperty(idPropName).GetValue(obj(i), Nothing)), _
CStr(obj(i).GetType().GetProperty(namePropName).GetValue(obj(i), Nothing)))
Of course, you could still have the first parameter by Object, and then attempt to cast to IList, but that's up to you.
ctype is used to convert in object type.