Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367 C# – Scattered Code

C# MVC Drop down List for Months

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367
public IEnumerable Months
{
    get 
    {
        return DateTimeFormatInfo
               .InvariantInfo
               .MonthNames
               .Select((monthName, index) => new SelectListItem
               {
                   Value = (index + 1).ToString(),
                   Text = monthName
               });
    }
}

C# MD5 Hash

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

Quick script for generating an MD5 Hash

private string ComputeHash(string input)
{
  using (var md5 = System.Security.Cryptography.MD5.Create())
  {
    var data = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
    
    var sb = new StringBuilder();
    
    foreach (var c in data) {
        sb.Append(c.ToString("x2"));
    }
    
    return sb.ToString();
  }
}

Value types, reference types and practical uses

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

C# has two different types of variables: value types and reference types. While in C and C++ primitive types can contain values or references and certain complex types (arrays, objects) can only be used via reference, in C# the line between the two types is very clear. Numeric types(int, decimal, double, etc.), bool and structs access the values directly. Class, object, interface, delegate string and dynamic are only accessed and used via reference. Because of all the awkwardness with referencing and dereferencing in C and C++, C# uses the 'ref'...

Localization in c#

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

Localization in C# is very straightforward. C# comes with Resource files, which are great because they can be easily translated, and C# can switch between them automatically.   First, add a new Resource File to your project. In my case, I called it SiteFields.resx. This will be the default language file, which in this case is English.   Next, we create a Localized Display Name Attribute, so we can leverage our ViewModels properly, instead of relying on text. public class LocalizedDisplayNameAttribute : DisplayNameAttribute { public LocalizedDisplayNameAttribute(string resourceId) : base(GetMessageFromResource(resourceId)) {...

Accessing indesign server from C# via COM

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

I've done quite a bit of searching on InDesign Server, and according to the people at Adobe, C# isn't "dynamic" enough for use with their API. But obviously, they were looking at Java documentation mislabeled C# or at some very old books. Anyway, a lot of things in their documentation are misleading, so I will attempt to get you started on the right path to using InDesign Server CS6 from C# via COM objects.   First thing I did was install the trial, which needs to be activated before the...

C# Thumbnail

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

In this tutorial I'll share how I create a c# thumbnail for each photo on our websites, and keep track of how many different sizes I need to use, while keeping images small for efficient load time, and efficient for server load. I will attach the Thumbnailer class at the end of the post. The thumbnailer can handle fixed width & height risize/crop, proportional resizing, width & height constraint resizing. The Thumbnailer can be used directly as an IHttpHandler or as a helper class for for another handler in your...

C# Serializable Dictionary

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

The Dictionary class included in C# isn't serializable, so you can use the following Serializable Dictionary whenever you need to serialize your objects [Serializable] public class SerializableDictionary : Dictionary, IXmlSerializable, ISerializable { #region Private Properties protected XmlSerializer ValueSerializer { get { return _valueSerializer ?? (_valueSerializer = new XmlSerializer(typeof(TVal))); } } private XmlSerializer KeySerializer { get { return _keySerializer ?? (_keySerializer = new XmlSerializer(typeof(TKey))); } } #endregion #region Private Members private XmlSerializer _keySerializer; private XmlSerializer _valueSerializer; #endregion #region Constructors public SerializableDictionary() { } public SerializableDictionary(IDictionary dictionary) : base(dictionary) { } public...

Compiling code on the fly in C#

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

Compile and run code created on the fly from a string or file. Compiler c = new Compiler(); StreamReader reader = new StreamReader(/*Path to my source file, optional*/); string compilationsource = reader.ReadToEnd(); //read from file or create on-the-fly CodeContainer result = new CodeContainer(); result.CompiledAssembly = c.Compile(compilationsource); result.SourceCode = compilationsource; result.Execute("MyMethod", new object[] { MyClass }); //Send in any parameters you need A lot more functionality than presented above is possible. To make this possible, you'll need the following Compiler class using System; using System.Data; using System.Configuration; using System.Collections; using System.Collections.Generic;...

C# CSV files

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

This NuGet package I found calle CSVHelper, lets you create an object representation of a CSV file, to read and write CSV files with ease, and use linq on them.   You first need to get CsvHelper from NuGet Add a reference to the libraries you got using CsvHelper; using CsvHelper.Configuration;   Create a Model to match the CSV file. You can reference columns by Index or Name (You can only reference by name if the CSV file contains a header) using System; using System.Collections.Generic; using System.Linq; using System.Text; using...

C# Reflection

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

C# Reflection copy every property of a class to another class with the same properties.   Let's say you have to copy all the values of the properties of a class to another (maybe larger) class, but there's no inheritance between them. You can achieve this using reflection very simply. foreach (var prop in item.GetType().GetProperties()) { var p2 = outputModel.GetType().GetProperty(prop.Name); p2.SetValue(outputModel, prop.GetValue(item, null), null); }