Convert IP to Long and Vice Versa C#

The inbuilt function in System.Net.IPAddress(IPAddr).ToString() method returns IP’s in the reverse order, hence I had to look for alternatives. Here is the correct version to convert IP address to long and viceversa

   1: static public string LongToIP(long longIP)
   2: {
   3:     string ip = string.Empty;
   4:     for (int i = 0; i < 4; i++)
   5:     {
   6:         int num = (int)(longIP / Math.Pow(256, (3 - i)));
   7:         longIP = longIP - (long)(num * Math.Pow(256, (3 - i)));
   8:         if (i == 0)
   9:             ip = num.ToString();
  10:         else
  11:             ip  = ip + "." + num.ToString();
  12:     }
  13:     return ip;
  14: }
  15:  
  16: public static long IP2Long(string ip)
  17: {
  18:     string[] ipBytes;
  19:     double num = 0;
  20:     if(!string.IsNullOrEmpty(ip))
  21:     {
  22:         ipBytes = ip.Split('.');
  23:         for (int i = ipBytes.Length - 1; i >= 0; i--)
  24:         {
  25:             num += ((int.Parse(ipBytes[i]) % 256) * Math.Pow(256, (3 - i)));
  26:         }
  27:     }
  28:     return (long)num;
  29: }

Using GZipStream to zip all files in folder

There is a neat little trick that one has to follow inorder to zip a entire folder into a gzip file.

It is essentially a two step process

  • Zip individual files in a folder using TarArchive
  • Zip the tar file using GZip
   1: public static string CreateTar(string directoryToCompress, string destPath, string tarFile)
   2: {
   3:     string destDrive = destPath.Substring(0, destPath.IndexOf(@"\") + 1);
   4:     Directory.SetCurrentDirectory(destDrive);
   5:     string tarFilePath = Path.Combine(destPath, tarFile);
   6:     using (Stream fs = new FileStream(tarFilePath, FileMode.OpenOrCreate))
   7:     {
   8:         using (TarArchive ta = TarArchive.CreateOutputTarArchive(fs))
   9:         {
  10:             string[] files = Directory.GetFiles(directoryToCompress);
  11:             foreach (string file in files)
  12:             {
  13:                 string entry = file.Substring(file.IndexOf(@"\") + 1);
  14:                 TarEntry te = TarEntry.CreateEntryFromFile(entry);
  15:                 ta.WriteEntry(te, false);
  16:             }
  17:         }
  18:     }
  19:     return tarFilePath;
  20: }

The above code creates the tar File. Note to create the Tar using TarArchive I used ICSharpCode.SharpZipLib.dll

The following code Zips the Tar file into a Gzip file:

   1: public static void CreateTarGzip()
   2: {
   3:     string sourceFolder = @"C:\beehive\stagingwebevent\20090324_00\";
   4:     string file = CreateTar(sourceFolder, @"C:\core\", "test.tar");
   5:     string outputFile = @"C:\core\test.tgz";
   6:     using (FileStream fs = new FileStream(outputFile, FileMode.CreateNew))
   7:     using (GZipStream s = new GZipStream(fs, CompressionMode.Compress))
   8:     {
   9:         using (FileStream inputfs = new FileStream(file, FileMode.Open))
  10:         {
  11:             byte[] buffer = new byte[4096];
  12:             int len = 0;
  13:             while ((len = inputfs.Read(buffer, 0, buffer.Length)) > 0)
  14:             {
  15:                 s.Write(buffer, 0, len);
  16:             }
  17:         }
  18:     }
  19: }

Some tips on .NET Memory usage

  • Always try to use string.Compare() method for testing string equality and inequality
  • use string.CompareOrdinal() method for case sensitive string comparisons
  • when returning string from a method call insure that you never return “null” return string.Empty instead
  • Always initialize strings to string.Empty
  • when instantiating StringBuilder use the overloaded constructor to specify the initial capacity
  • Prefer string.format when concatenating value types into strings
   1: int number = 4545;
   2: string x = string.Format("{0} is a even number", number);
   3: //above is better than using
   4: string y = number.ToString() + " is a even number";

  • Use BitArray instead of using Array of booleans
  • use for loop for iterating over arrays, use foreach for iterating over collections
  • Set initial capacity for StringBuilder, SortedList, HashTable, ArrayList, Queue, Stack etc
  • First add all elements to HashTable and then create Sortedlist from this HashTable, since adding to SortedList directly is very expensive
  • Use DictionaryEntry to iterate over Hastable entries
  • Garbage Collector settings: In .Net there are 2 versions of the garbage collector, Workstation Garbage collector (used on single processor systems) and Server garbage collector (used on multi processor systems. Server garbage collector pauses app during GC and uses 1 thread and 1 managed heap for each CPU. The workstation garbage collector minimizes the pauses since it runs the GC concurrently with the app’s worker threads.
  • On single CPU machine, Workstation GC and Server GC behave the same way, but you can prevent the GC from running concurrently with the App threads by using this config in app.config or machine.config:
   1: <configuration>
   2:     <runtime>
   3:         <gcConcurrent enabled="false"/>
   4:     </runtime>
   5: </configuration>

  • By default, non-interactive apps like Windows Services, .NET Remoting apps use Workstation GC. Force such apps (running on multi core CPU machines) to use the server GC by adding the following to app.config:
   1: <configuration>
   2:     <runtime>
   3:         <gcServer enabled="true"/>
   4:     </runtime>
   5: </configuration>

  • WekReference: Use WeaKReference to create cache versions of your objects. weak references are references to objects that last until the point that GC does not collect them, once GC runs, then these objects need to be repopulated from original source. Here is an example of cached File:
   1: public class CachedTextFile
   2: {
   3:     public readonly string FileName;
   4:     WeakReference wrText;
   5:     public CachedTextFile(string filename)
   6:     {
   7:         this.FileName = filename;
   8:     }
   9:  
  10:     private string ReadFile()
  11:     {
  12:         string text = string.Empty;
  13:         using (StreamReader sr = new StreamReader(FileName))
  14:         {
  15:             text = sr.ReadToEnd();
  16:         }
  17:         wrText = new WeakReference(text);
  18:         return text;
  19:     }
  20:     
  21:     public string GetText()
  22:     {
  23:         object text = null;
  24:         //check weak reference
  25:         if (wrText != null)
  26:             text = wrText.Target;
  27:         if (text != null)
  28:         {
  29:             //string still in cache
  30:             return text.ToString();
  31:         }
  32:         else
  33:         {
  34:             //read from disk
  35:             return ReadFile();
  36:         }
  37:     }
  38: }
  39:  
  40:  
  41:         CachedTextFile cacheFile = new CachedTextFile(@"C:\temp\test.txt");
  42:        // this is the first call hence gets the text from disk, instead of memory/cache
  43:         Console.WriteLine(cacheFile.GetText());
  44:         //force garbage collection manually
  45:         GC.Collect();
  46:         GC.WaitForPendingFinalizers();
  47:         //following causes the app to read the file again
  48:         Console.WriteLine(cacheFile.GetText());
  • Dont create/destroy large objects (objects with size greater than 85000bytes) frequently, since such objects get allocated on the Large Object Heap which isn’t freed by the GC. Consider splitting large objects into 2 or more smaller objects like Partitioned Array’s.

Dispose and Finalize methods

Here are are few tips on using the IDisposable pattern and using Finalizers

  • Implement IDiposable if accessing managed resources like SQLConnection/Command objects and not disposing them from within the method which instantiates the managed resource
  • Implement Finalizer only if using unmanaged resources (Files, Streams, Windows Handles, Pinvoke) and not disposing them from with the method that instantiates the unmanaged resources
  • Insure that each Type that has a finalizer is responsible for disposing only 1 unmanaged resource, and never reference “reference types” from within a Finalizer
  • Use a protected “disposed” bool within a IDiposable type and check its value in the beginning of each method of that type
  • If a type has a “Open” method which opens a managed resource, then insure that you Implement a Close() method which calls the explicit implementation of IDisposable.Dispose() method
   1: public class EncryptedStream : IDisposable
   2: {
   3:     public void Close()
   4:     {
   5:         (this as IDisposable).Dispose();
   6:     }
   7:  
   8:     void IDisposable.Dispose()
   9:     {
  10:        //Release resources
  11:     }
  12: }

  • If the Dispose() method can be called from multiple threads then insure that you use lock(this) before releasing resources. Here is a correct implementation of IDisposable and Finalize pattern which has unmanaged and managed resources to release:
   1: public class DisposeType : IDisposable
   2: {
   3:     protected bool disposed;
   4:     //insure the access modifier is protected so that derived classes can also make use of the same    
   5:     protected virtual void Dispose(bool disposing)
   6:     {
   7:         if (disposed)
   8:             return;
   9:  
  10:         lock (this)
  11:         {
  12:             if (disposing)
  13:             {
  14:                 // Release Managed resources here
  15:             }
  16:             // Release UnManaged resources here
  17:             //call the base object's Dispose protected method
  18:             base.Dispose(disposing);
  19:             disposed = true;
  20:         }
  21:     }
  22:  
  23:     public void Dispose()
  24:     {
  25:         Dispose(true);
  26:         GC.SuppressFinalize(this);
  27:     }
  28:  
  29:     ~DisposeType()
  30:     {
  31:         Dispose(false);
  32:     }
  33: }
  • Compound Finalizable objects: If you have a finalizable object that occupies lot of memory then consider splitting it into 2 classes, one that contains the finalizable object and other that contains the high memory using variables:
   1: public class CompoundObject : IDisposable
   2: {
   3:     //this array takes lot of memory
   4:     int[] array;
   5:     //instance of inner finalizable object
   6:     ClipBoardWrapper cwrapper;
   7:  
   8:     public CompoundObject(int hwnd, int elements)
   9:     {
  10:         array = new int[elements];
  11:         cwrapper = new ClipBoardWrapper(hwnd);
  12:     }
  13:  
  14:     public void Dispose()
  15:     {
  16:         cwrapper.Dispose();
  17:     }
  18:  
  19:     private class ClipBoardWrapper : IDisposable
  20:     {
  21:         [System.Runtime.InteropServices.DllImport("user32")]
  22:         private static extern int OpenClipboard(int hwnd);
  23:  
  24:         [System.Runtime.InteropServices.DllImport("user32")]
  25:         private static extern int CloseClipboard();
  26:         
  27:         public ClipBoardWrapper(int hwnd)
  28:         {
  29:             OpenClipboard(hwnd);
  30:         }
  31:         
  32:         public void Dispose()
  33:         {
  34:             CloseClipboard();
  35:             GC.SuppressFinalize(this);
  36:         }
  37:         ~ClipBoardWrapper()
  38:         {
  39:             Dispose();
  40:         }
  41:  
  42:     }
  43: }

Dynamic log fileNames with log4net

If you needed to generate dynamic logfile names with log4net then you can use the following config

   1: <appender name="RollingFileAppenderV1" type="log4net.Appender.RollingFileAppender">
   2:     <file type="log4net.Util.PatternString" value="F:\HornetFeed\%property{LogName}" />
   3:     <appendToFile value="true" />
   4:     <rollingStyle value="Size" />
   5:     <maxSizeRollBackups value="-1" />
   6:     <maximumFileSize value="5000KB" />
   7:     <staticLogFileName value="true" />
   8:     <countDirection value="1"/>
   9:     <layout type="log4net.Layout.PatternLayout">
  10:         <conversionPattern value="%m%n" />
  11:     </layout>
  12:     <filter type="log4net.Filter.PropertyFilter">
  13:         <Key value="Version" />
  14:         <StringToMatch value="1" />
  15:     </filter>
  16:     <filter type="log4net.Filter.DenyAllFilter" />
  17: </appender>

Note the %property{LogName} this is a log4net Property which we can set at runtime using C# code.

   1: log4net.GlobalContext.Properties["LogName"] = "file1.log";

Remember to set the GlobalContext Properties before instantiating the log4net logger. i.e. before this call:

   1: log4net.ILog log = LogManager.GetLogger(typeof(Program));

Also note the “PropertyFilter”

   1: <filter type="log4net.Filter.PropertyFilter">
   2:     <Key value="Version" />
   3:     <StringToMatch value="1" />
   4: </filter>

This Property is also set dynamically at runtime using C# code. We can use ThreadContext to determine which logfile the log entry will be written to. Thus if you wanted that the RollingFileAppenderV1 was used for writing insure that the Thread calling the log.Warn method precedes the call with this following line

   1: log4net.ThreadContext.Properties["Version"] = "1";

If you wanted to use RollingFileAppenderV1 always for logging all messages other than Exception messages then use the following

   1: try
   2: {
   3:     log4net.GlobalContext.Properties["Version"] = "1";
   4:     //Business logic with many log.Warn calls
   5: }
   6: catch (Exception ex)
   7: {
   8:     LogError(ex);
   9: }
  10:  
  11: ///Helper method to log errors:
  12: internal static void LogError(Exception ex)
  13: {
  14:     string state = "1";
  15:     if(log4net.ThreadContext.Properties["Version"] != null)
  16:         state = log4net.ThreadContext.Properties["Version"].ToString();
  17:     log4net.ThreadContext.Properties["Version"] = "0";
  18:     logger.HandleException(ex, "Error");
  19:     log4net.ThreadContext.Properties["Version"] = state;
  20: }

Note I am using GlobalContext and hence all threads will use this setting to write to log files. No need to set the TrheadContext.Properties on individual threads.

But I prefer to use ThreadContext so that we can control the setting on each thread level.

Also make sure the log4net XML config is set as follows:

   1: <log4net>
   2: // the following logs only those logs when the Property Version is set to 1 i.e.
   3: // if Version =1  then log.Warn calls are logged to this dynamically named file
   4:     <appender name="RollingFileAppenderV1" type="log4net.Appender.RollingFileAppender">
   5:         <file type="log4net.Util.PatternString" value="F:\HornetFeed\%property{LogName}" />
   6:         <appendToFile value="true" />
   7:         <rollingStyle value="Size" />
   8:         <maxSizeRollBackups value="-1" />
   9:         <maximumFileSize value="5000KB" />
  10:         <staticLogFileName value="true" />
  11:         <countDirection value="1"/>
  12:         <layout type="log4net.Layout.PatternLayout">
  13:             <conversionPattern value="%m%n" />
  14:         </layout>
  15:         <filter type="log4net.Filter.PropertyFilter">
  16:             <Key value="Version" />
  17:             <StringToMatch value="1" />
  18:         </filter>
  19:         <filter type="log4net.Filter.DenyAllFilter" />
  20:     </appender>
  21:  
  22: //The following matches and logs all events except Version=1 and Version=2 and strings containing CACHE_CALL_LOG
  23:  
  24:     <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  25:         <file type="log4net.Util.PatternString" value="F:\logfiles\trace.log" />
  26:         <appendToFile value="true" />
  27:         <rollingStyle value="Size" />
  28:         <maxSizeRollBackups value="10" />
  29:         <maximumFileSize value="3000KB" />
  30:         <staticLogFileName value="true" />
  31:         <countDirection value="1"/>
  32:         <layout type="log4net.Layout.PatternLayout">
  33:             <conversionPattern value="%d [%t] %-5p %c [%x] - %m%n" />
  34:         </layout>
  35:         <filter type="log4net.Filter.PropertyFilter">
  36:             <Key value="Version" />
  37:             <StringToMatch value="1" />
  38:             <acceptOnMatch value="false" />
  39:         </filter>
  40:         <filter type="log4net.Filter.PropertyFilter">
  41:             <Key value="Version" />
  42:             <StringToMatch value="2" />
  43:             <acceptOnMatch value="false" />
  44:         </filter>
  45:         <filter type="log4net.Filter.StringMatchFilter">
  46:             <stringToMatch value="CACHE_CALL_LOG" />
  47:             <acceptOnMatch value="false" />
  48:         </filter>
  49:     </appender>

Tips on using log4net RollingFileAppender

By Default we have the following values for following properties of RollingFileAppender

  • staticLogFileName = true
  • countDirection = –1
  • rollingStyle = Composite
  • maxSizeRollBackups = 0 // be careful with this
  • maximumFileSize = “10MB”
  • datePattern = ".yyyy-MM-dd"

staticLogFileName indicates whether you need to keep writing (log) to the same file all the time. You will need to set it to false when using Date as the rolling style and you have large number of backups.

Optionally file.log.yyyy-mm-dd for current formated datePattern can by the currently logging file (or file.log.curSizeRollBackup (rollingStyle=Size) or even file.log.yyyy-mm-dd.curSizeRollBackup --- (rollingStyle=Composite)) This will make time based roll overs with a large number of backups much faster -- it won't have to rename all the backups!

Recommend to leave it at its default value “true”

countDirection when its value is –1, then newest logfile backup will always be file.log.1.. hence this would involve more number of file renaming.

By default newer files have lower numbers. (countDirection < 0) ie. log.1 is most recent, log.5 is the 5th backup, etc... countDirection > 0 does the opposite ie. log.1 is the first backup made, log.5 is the 5th backup made, etc. For infinite backups use countDirection > 0 to reduce rollOver costs.

rollingStyle can be either Date, Size or Composite. the default setting Composite, uses a combination of Size and Date settings. Thus if you have the datePattern set to “.yyyy-MM-dd” and maxSizeRollBackups set to 10, themn it will maintain 10 log backups for each day.

If you have the DatePattern set to “.yyyy-MM-dd HH:mm” and maxSizeRollbackups = 10 then it will maintain 10 logfile backups per minute

Samples:

   1: <appender name="RollingFileAppenderV1" type="log4net.Appender.RollingFileAppender">
   2:     <file type="log4net.Util.PatternString" value="F:\HornetFeed\%property{LogName}" />
   3:     <appendToFile value="true" />
   4:     <rollingStyle value="Size" />
   5:     <maxSizeRollBackups value="-1" />
   6:     <maximumFileSize value="5000KB" />
   7:     <staticLogFileName value="true" />
   8:     <countDirection value="1"/>
   9:     <layout type="log4net.Layout.PatternLayout">
  10:         <conversionPattern value="%m%n" />
  11:     </layout>
  12:     <filter type="log4net.Filter.PropertyFilter">
  13:         <Key value="Version" />
  14:         <StringToMatch value="1" />
  15:     </filter>
  16:     <filter type="log4net.Filter.DenyAllFilter" />
  17: </appender>

This will create infinite file backups with the countdirection > 0 so that the newest file has the latest/greatest name i.e. log.5 for the newest backup (5th backup)

   1: <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
   2:         <file value="logfile" />
   3:         <appendToFile value="true" />
   4:         <rollingStyle value="Composite" />
   5:         <datePattern value=".yyyyMMdd-HHmm" />
   6:         <maxSizeRollBackups value="10" />
   7:         <maximumFileSize value="1MB" />
   8:         <countDirection value="1"/>
   9:         <layout type="log4net.Layout.PatternLayout">
  10:             <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
  11:         </layout>
  12:     </appender>

This is a Composite RollingFileAppender which keeps max of 10 1MB log backups every minute

ConfigSectionHandler for Hierarchical configs

Here is a quick way to create a ConfigSection Handler for reading Hierarchical configs. For e.g. suppose you need to read a config file which has the following structure:

   1: <MainConfig>
   2:     <Company>MySpace</Company>
   3:     <SubConfig>
   4:         <ID>23</ID>
   5:         <Name>Rohit</Name>
   6:         <LastName>Gupta</LastName>
   7:     </SubConfig>
   8: </MainConfig>

For this you would create the 2 classes, one for the Parent Config and another for the child config like this:

   1: [XmlRoot("MainConfig")]
   2:  public class MainConfig
   3:  {
   4:      private static readonly MainConfig instance = (MainConfig)ConfigurationManager.GetSection("MainConfig");
   5:      public static MainConfig GetInstance()
   6:      {
   7:          if (instance == null)
   8:              throw new ConfigurationErrorsException(
   9:                  "Unable to locate or deserialize the 'SearchRecommendationsConfiguration' section.");
  10:  
  11:          return instance;
  12:      }
  13:  
  14:      public string Company { get; set; }
  15:  
  16:      public SubConfig FriendsConfig { get; set; }
  17:  }
  18:  
  19:  [XmlRoot("SubConfig")]
  20:  public class SubConfig
  21:  {
  22:      public int ID { get; set; }
  23:      public string Name { get; set; }
  24:      public string LastName { get; set; }
  25:  }

Then you would create a configsectionHandler class which reads the config from the .config file:

   1: public class MainConfigSectionHandler : IConfigurationSectionHandler
   2: {
   3:     #region IConfigurationSectionHandler Members
   4:  
   5:     public object Create(object parent, object configContext, System.Xml.XmlNode section)
   6:     {
   7:         MainConfig typedConfig = GetConfig<MainConfig>(section);
   8:  
   9:         if (typedConfig != null)
  10:         {
  11:             #region Get Sub Configs
  12:             foreach (XmlNode node in section.ChildNodes)
  13:             {
  14:                 switch (node.Name)
  15:                 {
  16:                     case "SubConfig":
  17:                         SubConfig friendsConfig = GetConfig<SubConfig>(node);
  18:                         typedConfig.FriendsConfig = friendsConfig;
  19:                         break;
  20:                     default:
  21:                         break;
  22:                 }
  23:             }
  24:             #endregion
  25:         }
  26:  
  27:  
  28:         return typedConfig;
  29:     }
  30:  
  31:     public T GetConfig<T>(System.Xml.XmlNode section) where T : class
  32:     {
  33:         T sourcedObject = default(T);
  34:         Type t = typeof(T);
  35:         XmlSerializer ser = new XmlSerializer(typeof(T));
  36:         sourcedObject = ser.Deserialize(new XmlNodeReader(section)) as T;
  37:         return sourcedObject;
  38:     }
  39:  
  40:     #endregion
  41:  
  42: }

After this you would add a entry in the app.config for this configsection Handler as the following:

   1: <configSections>
   2:     <section name="MainConfig" type="App.MainConfigSectionHandler,App" />
   3: </configSections>

Finally to read this config, you would write the following:

   1: MainConfig config = MainConfig.GetInstance();
   2: Console.WriteLine(config.Company);
   3: Console.WriteLine(config.FriendsConfig.ID);
   4: Console.WriteLine(config.FriendsConfig.LastName);
   5: Console.WriteLine(config.FriendsConfig.Name);

Lucene: Multifield searches

We can run multifield searches in Lucene using either the BooleanQuery API or using the MultiFieldQueryParser for parsing the query text. For e.g. If a index has 2 fields FirstName and LastName and if you need to search for "John" in the FirstName field and "Travis" in the LastName field one can use a Boolean Query as such:

   1: BooleanQuery bq = new BooleanQuery();
   2: Query qf = new TermQuery(new Lucene.Net.Index.Term("FirstName", "John"));
   3: Query ql = new TermQuery(new Lucene.Net.Index.Term("LastName", "Travis"));
   4: bq.Add(qf, BooleanClause.Occur.MUST);
   5: bq.Add(ql, BooleanClause.Occur.MUST);
   6: IndexSearcher srchr = new IndexSearcher(@"C:\indexDir");
   7: srchr.Search(bq);

Now if we need to search a single term across either of the FirstName and LastName fields then we can use the MultiFieldQueryParser as follows:

   1: Query query = MultiFieldQueryParser.parse("commonName", new String[] { "FirstName", "LastName" },new SimpleAnalyzer());
   2: srchr.Search(query);
Now if you need to search the term that must exist in both the fields then we use the following:
   1: Query query = MultiFieldQueryParser.Parse("commonName",
   2:                         new String[] { "FirstName", "LastName" },
   3:                         new BooleanClause.Occur[] { BooleanClause.Occur.MUST,BooleanClause.Occur.MUST},
   4:                         new SimpleAnalyzer());
   5: srchr.Search(query);

Finally if you don’t want a term to occur in one of the Fields (say FirstName) then use:

   1:  
   2:                 Query query = MultiFieldQueryParser.Parse("commonName",
   3:                                         new String[] { "FirstName", "LastName" },
   4:                                         new BooleanClause.Occur[] { BooleanClause.Occur.MUST_NOT,BooleanClause.Occur.MUST},
   5:                                         new SimpleAnalyzer());
   6:                 srchr.Search(query);

so if you need to search a single term across multiple fields then use MultiFieldQueryParser, if you need to search different terms in different fields then use the BooleanQuery as shown first

Dynamically creating types using reflection and setting properties using Reflection.Emit.

I came across a requirement where we needed to create types dynamically based on XML Configuration files, so that in the furture new types are required we dont need to update the application again by creating a new class.
The additional requirement was to populate the property names of the class based on the Cml configuration and its values using the Querystring values from the HttpWebRequest.
I earlier thought about using Dynamic methods from .NET Framework 2.0, but that did not fit my purpose since I didn't need to execute methods from the dynamic type that was created.

So here is the code:
I created three helper methods
1. Generates the AssemblyBuilder and Module Builder objects
2. Generates the TypeBuilder object.. which will be used for generating an instance of the dynamic type using Activator.CreateInstance
3. Create properties using Relection.Emit
   1: public class DynamicType
   2: {
   3:  
   4:     public static AssemblyBuilder asmBuilder = null;
   5:     public static ModuleBuilder modBuilder = null;
   6:     public static void GenerateAssemblyAndModule()
   7:     {
   8:         if (asmBuilder == null)
   9:         {
  10:             AssemblyName assemblyName = new AssemblyName();
  11:             assemblyName.Name = "DWBeacons";
  12:             AppDomain thisDomain = Thread.GetDomain();
  13:             asmBuilder = thisDomain.DefineDynamicAssembly(
  14:                          assemblyName, AssemblyBuilderAccess.Run);
  15:             modBuilder = asmBuilder.DefineDynamicModule(
  16:                          asmBuilder.GetName().Name, false);
  17:         }
  18:     }
  19:  
  20:     public static TypeBuilder CreateType(ModuleBuilder modBuilder, string typeName)
  21:     {
  22:         TypeBuilder typeBuilder = modBuilder.DefineType(typeName,
  23:                     TypeAttributes.Public |
  24:                     TypeAttributes.Class |
  25:                     TypeAttributes.AutoClass |
  26:                     TypeAttributes.AnsiClass |
  27:                     TypeAttributes.BeforeFieldInit |
  28:                     TypeAttributes.AutoLayout,
  29:                     typeof(object));
  30:         return typeBuilder;
  31:     }
  32:  
  33:  
  34:     public static void CreateProperty(TypeBuilder t, string name, Type typ)
  35:     {
  36:         string field = "_" + name.ToLower();
  37:         FieldBuilder fieldBldr = t.DefineField(field, typ, FieldAttributes.Private);
  38:         PropertyBuilder propBldr = t.DefineProperty(name, PropertyAttributes.HasDefault, typ, null);
  39:         MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
  40:  
  41:         MethodBuilder getPropBldr = t.DefineMethod("get_" + name, getSetAttr, typ, Type.EmptyTypes);
  42:  
  43:         ILGenerator getIL = getPropBldr.GetILGenerator();
  44:         getIL.Emit(OpCodes.Ldarg_0);
  45:         getIL.Emit(OpCodes.Ldfld, fieldBldr);
  46:         getIL.Emit(OpCodes.Ret);
  47:  
  48:         MethodBuilder setPropBldr = t.DefineMethod("set_" + name, getSetAttr, null, new Type[] { typ });
  49:  
  50:         ILGenerator setIL = setPropBldr.GetILGenerator();
  51:  
  52:         setIL.Emit(OpCodes.Ldarg_0);
  53:         setIL.Emit(OpCodes.Ldarg_1);
  54:         setIL.Emit(OpCodes.Stfld, fieldBldr);
  55:         setIL.Emit(OpCodes.Ret);
  56:  
  57:         propBldr.SetGetMethod(getPropBldr);
  58:         propBldr.SetSetMethod(setPropBldr);
  59:  
  60:     }
  61:  
  62: }

Then to use these helper methods from the Main program I used the following:
   1: if (DynamicType.asmBuilder == null)
   2:     DynamicType.GenerateAssemblyAndModule();
   3: finalType = DynamicType.modBuilder.GetType("Beacon11");
   4:  
   5: TypeBuilder tb = DynamicType.CreateType(DynamicType.modBuilder, typeName);
   6:  
   7: foreach (XElement e in beaconNode.Descendants())
   8: {
   9:     string pname = e.Attribute("qs").Value;
  10:     string ptype = e.Attribute("type").Value;
  11:     DynamicType.CreateProperty(tb, pname, Type.GetType(ptype));
  12: }
  13: finalType = tb.CreateType();
  14:  
  15: Object obj = Activator.CreateInstance(finalType);
  16: // this sets the properties of the just instantiated class
  17: finalType.InvokeMember("bv", BindingFlags.SetProperty, null, data, new object[] { 1.0 });
  18: finalType.InvokeMember("tp", BindingFlags.SetProperty, null, data, new object[] { 2.0 });
  19: //this sets the properties of the type by using values from the querystring
  20: foreach (XElement e in beaconNode.Descendants())
  21: {
  22:     string pname = e.Attribute("qs").Value;
  23:     object value = context.Request.QueryString[pname];
  24:     finalType.InvokeMember(pname, BindingFlags.SetProperty, null, data, new object[] { value });
  25: }
For more information visit : msdn - PropertyBuilder

Schedule a task using C# code

I needed to schedule a task to run every day at 9:00 p.m. in the night. I had an addition al requirement that the task be scheduled only if the FileSystemwatcher alerts us of new files being available for processing.

Thus the files could be recieved anytime during the day, but despite that the task should be schduled to run exactly at 9:00 p.m. in the night.

So I used the following code to schedule a task(using System.Threading.Timer and TimeSpan classes)

DateTime d = DateTime.Now;
timer = new Timer(new TimerCallback(Update), null, 
TimeSpan.FromMinutes(21 * 60 - (d.Hour * 60 + d.Minute)), 
TimeSpan.FromMilliseconds(-1));

Note I used TimeSpan.FromMillisecnods(-1) to disable periodic signalling

Compare 2 SQL queries for equality

DECLARE @check1 bigint
DECLARE @check2 bigint

Select @check1 = CHECKSUM_AGG(CHECKSUM(*))
FROM
(
    SELECT *
    FROM dbo.Orders (nolock)
)
AS Source

Select @check2 = CHECKSUM_AGG(CHECKSUM(*))
FROM
(
    SELECT *
    FROM dbo.Orders (nolock)
    WHERE IsSuccessful = 1
)
As Comparison

IF @check1 = @check2
    PRINT 'Queries are Equal'
ELSE
    PRINT 'Queries are NOT Equal'
Note: If order of rows is different it will not effect the result It does not do execution plan comparisons, simply checks if the rows returned by the two queries are the same or not More info

Converting SQL2005 DBTimeStamp to Long for Comparison (Convert from Hex to long and decimal to Hex)

If you need to Convert SQL 2005 DB TimeStamp values to Long then follow these steps:
1. Retrieve the DB TimeStamp value in a Byte Array (8 bytes long).
2. Convert the byte array into a Hexadecimal string
3. Convert the hexadecimal string to a long
private long BytesToLong(byte[] bytes)
{
    string ts = null;
    foreach (byte b in bytes)
        ts += b.ToString("X").PadLeft(2,Convert.ToChar("0"));
    return Convert.ToInt64(ts, 16);
}
There is another method to convert the DBTimeStamp to Long, but I dont prefer this method since it can return negative values. The method is:
1. Retrieve the DB TimeStamp value in a Byte Array (8 bytes long). 2. Use BitConverter class to convert the byte array into long
long result = BitConverter.ToInt64(dbTimestamp, 0); 
Inorder to remedy this use the following to generate the same long value as in step 1:
public static long FromDbTimestamp(byte[] dbTimestamp)
{
    long result = 0;
    if (dbTimestamp != null)
    {
        Array.Reverse(dbTimestamp);
        result = BitConverter.ToInt64(dbTimestamp, 0);
    }

    return result;
}
Finally to convert a long into the correct DBTimeStamp use this:
public static byte[] ToDbTimestamp(long timestamp)
{
    byte[] result = BitConverter.GetBytes(timestamp);
    Array.Reverse(result);
    return result;
}

Linq To XML : Check for existence of element

var bookQuery = 

from book in bookXml.Descendants("Item")
let attributes = book.Element("ItemAttributes")

let price = Decimal.Parse((
  book.Elements("OfferSummary").Any() 
  && book.Element("OfferSummary").Elements("LowestNewPrice").Any()
  ? book.Element("OfferSummary")
    .Element("LowestNewPrice")
    .Element("Amount").Value
  : (attributes.Elements("ListPrice").Any()
      ? attributes.Element("ListPrice").Element("Amount").Value 
      : "0"))) / 100

select new {Price = price};

Elements(“node”).Any() does the trick.

Some other Linq tips: Some extensions to use with Linq include: Intersect, Union and Except. Intersect returns a IEnumerable collection which is a inner join between the 2 collections. Union returns a collection which is a full outer join between the 2 collections Except returns elements unique to collection1 that don’t exist in the collection2.

Method Hiding... Polymorphism in C#

In the following code:

interface IBand
{
int ID {get;set;}
string Name {get;set;}
void GetStatus();
}
public class Band : IBand
{
public int ID { get; set; }
public string Name { get; set; }

public Band()
{
GetStatus();
}
public void GetStatus()
{
ID = 555;
Name = "Ring";
Console.WriteLine("Base Class Called");
}
}

public class ABand : Band, IBand
{
public string Update { get; set; }

public ABand() : base() {}

public new void GetStatus()
{
ID = 655;
Name = "TEsst";
Update = "ddd";
Console.WriteLine("Derived class Called");
}
}
class Program
{
static void Main(string[] args)
{
IBand band = new ABand();
band.GetStatus();
}

}

We have 2 classes, Band and ABand which implement the IBand interface explicitly.

Now the call to band.GetStatus in Main will display "Derived class called". This will only happen if the Derived class (ABand) also implements the interface IBand explicitly (not in the literal sense in that it does not implement the methods using the IBand.GetStatus syntax)

If the ABand class does not implement the IBand interface explicitly (but implicitly since it inherits from Band which implements IBand) then the above code will output "Base class called"

However note one thing, that when the CTor for ABand is invoked it will invoke the ctor Band class and which will call GetStatus() method from within Band class hence this will output "Base class called" even though the ABand class has implemented the IBand interface explicilty.

Passing state to threads via anonymous calls

We already know that we can pass state into to Threads in 2 ways:

1. using ParameterizedThreadStart delegate

2.using global variables(reference or value types) accessible to the main thread as well the instantiated thread. This is also the way to share data amonst multiple threads. Infact the most common way to share data between threads is using static variables where application wide scope is desired.

3.using Thread.QueueUserWorkItem ... This differs from Parameterized ThreadStart delegate since it uses threads from a preconfigured ThreadPool instead of manually creating threads

There is another cool method to pass information to threads is via anonymous methods. For e.g.
static void Main
{
    string mainMessage = "Hello from Main !!!";
    Thread t = new Thread(delegate() { Print(mainMessage);});
    t.Start();
}
static void Print(string message)
{
    Console.Writeline(message);
}
Advantage of using this technique is that, it provides strongly typed access to the parameters passed to the thread. The Target Method can accept any number of arguments, and no casting is required (unlike Parameterized Start where the passed state is an “object”) . The flip side, though, is that you must keep outer-variable semantics in mind
Twitter