ExpandoObject PropertyChanged event not triggering in propertygrid - vb.net

Basically I am loading a JSON object that contains combinations of values available or not at run time, so I need to know when a specific property is modified to then toggle all the other browsable etc.. and though that the PropertyChange event was the perfect way to handle it.
So I can add an event handler to get triggered on my expandoobject like this:
Dim test As Object = new ExpandoObject
AddHandler CType(test, INotifyPropertyChanged).PropertyChanged, AddressOf expando_PropertyChanged
and the handler is as basic as it gets
Public Shared Sub expando_PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
Debug.Print("Property {0} set or changed", e.PropertyName)
End Sub
so far this works, if I add or modify a property right after that, I get notified.
however if I return this and set it as the selectedobject of my propertygrid, I cannot get the event to trigger.
I'm using a custom PropertyDescriptor and ICustomTypeDescriptor to set a few other attributes for the propertygrid, so I assumed it might be as easy as setting the attribute
<RefreshProperties(RefreshProperties.All)>
but I cannot find a way to override the Refresh in the PropertyDescriptor unlike Browsable or readonly, which kinda makes sense as the property grid would need to know ahead of time that it needs to be refreshable.

So I could not make the INotifyPropertyChanged work with the expando, it would work with a dynamicObject where I would implement it myself but that was requiring too much of a rewrite for me.
I ended up add a lambda on my expando that I call on the PropertyDescriptor SetValue
CType(_expando, Object).toggleSwitches.Invoke(_expando, _name, value)
note the use of Invoke here in vb.net that was also a PITA but I found this guy who had the same issue as I did: https://github.com/dotnet/vblang/issues/226
It is not necessary to use invoke in C# and as 99% of the examples are in C# it took me more time than I wanted to implement it.
Hopefully this will help someone too.
here is the lambda if interested as well:
_expando.toggleSwitches = Sub(obj As Object, caller As String, value As Object)
Debug.Print(caller & " " & value.ToString())
End Sub

Related

Multi-Thread (Append Text To TextBox)

Done my fair share of looking this up but it just doesn't make sense..
I know we have to use delegates to update a textbox thats on the Main UI.
Here is the code in the a nutshell:
Initiate the thread that will capture chats:
ChatQuery = New Thread(AddressOf FetchChats)
ChatQuery.Start()
FetchChats Code Simplified:
SetTextBoxWithInvoke(Form5.TextBox2, MESSAGE)
SetTextBoxWithInvokeCode:
Private Sub SetTextBoxWithInvoke(ByVal TB As TextBox, ByVal msg As String)
If TB.InvokeRequired Then
TB.Invoke(New AddToMessageBoxDelegate(AddressOf SetTextBoxWithInvoke), New Object() {TB, msg})
Else
TB.Text &= msg
End If
End Sub
The Problem?? Invoke is never required, and the new message is never appended to the textbox I need to be appended to.
Delegate:
Public Delegate Sub AddToMessageBoxDelegate(ByVal TB As TextBox, ByVal msg As String)
The problem is that you're using the default instance of the form here:
SetTextBoxWithInvoke(Form5.TextBox2, MESSAGE)
Default instances are thread-specific so, if you execute that code on a background thread, rather than using the existing Form5 instance that was created and displayed on the UI thread, it will create a new instance on the current thread. There's no need to invoke a delegate to access that instance so InvokeRequired is always False.
You need to use the actual instance of Form5 that already exists. How exactly you do that depends on the circumstances. If the code that makes the call is already in that form then just use Me, which is implicit if you don't explicitly use another reference anyway. Otherwise, it's up to you to get the required reference into the required object to be used.
If the code is not in a form already then maybe you should not be doing it that way at all. Instead, you can use the SynchronizationContext class. You get the current instance when your object is created on the UI thread and you can then call its Send or Post method to marshal back to the UI thread without an explicit Control reference. That might not work in your case though, because you'd still need a reference to the correct TextBox.

How do I add a design-time description to a property implemented through an extender provider?

I know that I can add a design-time Designer description to a custom control's property by doing this:
<Category("Data"), Description("This describes this awesome property")>
Public Property Foo As Boolean
...
End Property
What I want to do is the exact same thing, but to properties that my extender provider component is providing other controls on my form with, so that when I click on the property's value field, for example, I would see the description I wrote for it. Searched a lot for an answer but had no success so far. Would I have to add something to my getter and setter methods for the property?
Thank you.
Would I have to add something to my getter and setter methods for the property?
Yes. Add the DescriptionAttribute to the Get[PropertyName] method. The same goes for any other Attributes (they dont seem to work on the Set... counterpart).
<Category("ListContolExtender"), DisplayName("DisplayMode"),
Description("My very clever description")>
Public Function GetDisplayMode(ctl As Control) As ItemDisplays
If extData.ContainsKey(ctl) Then
Return extData(ctl).DispMode
Else
Return ItemDisplays.Enabled
End If
End Function
Public Sub SetDisplayMode(ctl As Control, v As ItemDisplays)
If extData.ContainsKey(ctl) Then
extData(ctl).DispMode = v
Else
Dim e As New ExtenderData
e.DispMode = v
extData.Add(ctl, e)
End If
End Sub
The DisplayNameattribute hides all the DisplayMode on ListBoxExtender verbiage

Convert VB.net Code into C#.net

I am working on a C#.net. I have a code of vb.net and I want to convert those code into C#.
I have done all my task but while running application it gives me a error of object not set to an object reference. below is my VB.net code. I have used third party ddl in my code so ExchangeList is a class of that dll.
Private WithEvents moExchanges As ExchangeList
Private Sub RequestChartData()
Trace.WriteLine("Init")
moExchanges = moHost.MarketData.Exchanges
End Sub
Now below is my C#.code
Private Host moHost;
Private ExchangeList moExchanges;
private void RequestChartData()
{
Trace.WriteLine("Init");
moExchanges = moHost.MarketData.Exchanges;
}
Thanks.
It's not possible to tell you with 100% certainty the source of your object not set to an object reference. without seeing more code, but the error appears to be telling you that moHost is null - in other words, you haven't created an instance of the object yet.
So when you try to call MarketData.Exchanges on the object (so you can assign it to moExchanges, it's throwing the error.
Find moHost in your code and make sure you have that you have assigned an instance of that object to it (by calling it's constructor, like moHost = new Host() or whatever the constructor is), and this should fix your error.
UPDATE
You never initialize moHost. This line:
private Host moHost;
Simply declares the object - at this point it's null. So when you try to access any instance methods/properties, you get the object not set to an object reference error.
You need to create an instance of moHost by calling it's constructor, something like this:
private Host moHost = new Host();
The constructor may require parameters - take a look at the documentation for the third-party DLL (intellisense in the IDE may also tell you what parameters, if any, are needed).
This will take care of your error.
()Instead of:
Private Host moHost;
Private ExchangeList moExchanges;
I think you need to write it as:
Private Host moHost = New Host();
Private ExchangeList moExchanges = New ExhangeList();

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.

Threading: Call a delegate from a separate thread (VS2010)

So, I'm having troubles implementing a separate thread. This is because I have a simple class, and in it I start a new thread. So, as it is not any form, I haven't found any way to make it call the function in the UI Thread.
So, I cannot use the Invoke method. Is there any way to call a function from another thread?
I am going to assume that you have events exposed from your class and that you want the event handlers to execute on a UI thread. I suppose you could have a callback that the caller specifies as well. Either way the pattern I will describe below will work in both cases
One way to make this happen is to have your class accept an ISynchronizeInvoke instance. Form and Control instances implement this interface so a reference to one of them could be used. You could make it a convention that if the an instance is not specified then event handlers executed by raising events on your class would execute in the worker thread instead of the thread hosting the ISynchronizeInvoke instance (usually a form or control).
Public Class YourClass
Private m_SynchronizingObject As ISynchronizeInvoke = Nothing
Public Sub New(ByVal synchronizingObject As ISynchronizeInvoke)
m_SynchronizingObject = synchronizingObject
End Sub
Public Property SynchronizingObject As ISynchronizeInvoke
Get
Return m_SynchronizingObject
End Get
Set(ByVal value As ISynchronizeInvoke)
m_SynchronizingObject = value
End Set
End Property
Private Sub SomeMethodExecutingOnWorkerThread()
RaiseSomeEvent()
End
Private Sub RaiseSomeEvent()
If Not SychronizingObject Is Nothing AndAlso SynchronizingObject.InvokeRequired Then
SynchronizingObject.Invoke(New MethodInvoker(AddressOf RaiseSomeEvent)
End If
RaiseEvent SomeEvent
End Sub
End Class
The first thing to notice is that you do not have to specify a synchronizing object. That means you do not have to have a Form or Control reference. If one is not specified then SomeEvent will be raised on the worker thread. This is the same pattern that is used in the System.Timers.Timer class.
Try to expose some events in your class, fire them when you need to notify your UI and finally make your UI Component register to these events,
when the event is fired, the listener methods will be executed. there you can use Control.Invoke or Control.BeginInvoke to execute your code on the UI thread.