Main Site Documentation

Reflection & GetProperty


#1

It turns out that MS has decided not to include GetProperty function to NETMF while including GetMethod. I hardly can imagine what could be a reason for this, but anyway. I need to set property’s value using property’s name. Something like this:



In this case solutions like changing properties to methods, making a dictionary that includes all property' names and alike are not suitable.

What are my options? I need it badly :(

#2

I always used interfaces when going across app domains. You can search the forum for “code donation” to find the open source version of Tinkr.


#3

Can you please elaborate on that? How do you use Reflection to access Properties? How does Interfaces fit into this process?

Btw, Tinkr is placed here: https://github.com/osre77/Tinkr


#4

I don’t. What I’m saying is if you derived a class from an interface known to the calling app domain you could create it as an instance of the interface and use the associated properties.


#5

I want to make an universal plug-in, so I would prefer not to modify present code. That’s why I was looking into Reflection.

But if I agree to do a slight modification, how should it look like? Let’s take this example:

Partial Public Class Program
	Public Sub ProgramStarted()
		SetPropertyByName("SetMe", True)
	End Sub

	Sub SetPropertyByName(name As String, value As Boolean)
		' what should happen here?
	End Sub
End Class

Interface ITest
	WriteOnly Property SetMe() As Boolean
End Interface

Class TestClass
	Implements ITest

	Public WriteOnly Property SetMe As Boolean Implements ITest.SetMe
		Set(value As Boolean)
			Debug.Print("Done")
		End Set
	End Property
End Class

What should be done in [em]SetPropertyByName[/em] for this to work?


#6

@ iamin

You can call GetMethods() method of your Type instance. This method will return all methods that are available on the type. Including getters and setters for the properties.

Next you need to iterate through the returned array to find the proper MethodInfo for getter or setter.

Getter for property Name is get_Name. Setter is set_Name.


#7

Thank you 100 times! :dance:


#8

You are welcome! :slight_smile: