Posts

Showing posts with the label C#

Spotfire: How to add Gaussian Curve Fit to a chart using C# or IronPython code

Image
 Here is a Spotfire ScatterPlot with a Gaussian Curve.  It is straightforward to add it from the UI by right clicking on the chart, opening up properties dialog and adding a Gaussian Curve Fir from the Lines & Curves section: Here is how you would add it from the code:  var gaussianFittingModel = scatterPlot.FittingModels.AddNew<GaussianFittingModel>();

C# How to Serialize/Deserialize BitVector32

I have a class with a property of type BitVector32, when I Serialize it, I noticed the property was not getting serialized because the BitVector32 is not [Serializable]. After thinking about how can I serialize this property and looking into the implementation of BitVector32 it became clear that I need to approach this from a different angle. BitVector32 class has a property called Data where the information is stored, this property is of type Int and Int is obviously [Serializable]. Here is my approach: [ Serializable ]      public   class   TreeItem     {          // Serializable requires an empty constructor           public  TreeItem()         {         }          public   int ...

C# How to unit test Dispatcher

Dispatcher provides services for managing the queue of work items for a thread. Sometimes to execute a task on a background thread.  Any task executed by calling Dispatcher.BeginInvoke(...) in unit test does not get executed until you do some wire-up. This is how you would unit test code with Dispatcher. using  System; using  System.Windows.Threading; using  Microsoft.VisualStudio.TestTools.UnitTesting; namespace  UnitTesting.Tests {     [ TestClass ]      public   class   DispatcherUtilTest     {          private   ClassThatUsesDispatcher  _subject;         [ TestInitialize ]          public   void  TestInitialize()         {             _subject...

C# How to create generic List with dynamic type

Given the Type typeof( Item ) (defined below), how would you create a list of Items using Reflection in C#? var  type =  typeof ( Item ); IList  listOfItems = ( IList ) Activator .CreateInstance( typeof ( List <>).MakeGenericType(type));   public   class   Item {      public   string  Name {  get ;  set ; } }     

C# how to get type of generic class using reflection

In the following example, given an instance of SystemFolder, how would you find the generic type Folder given that SystemFolder  :  IDropTarget < Folder >? using  System; using  System.Collections.ObjectModel; using  System.Diagnostics; using  System.Linq; namespace  CSharpGenerics { public   class   GenericUtilities     {          // Example Type type = GenericUtilities.GetGenericType(new SystemFolder()); will return Folder          public   static   Type  GetGenericType( object  @object)         {              var  type = @object.GetType();              var  iDropTargetObject =...