Skip to content

Instantly share code, notes, and snippets.

View jltrem's full-sized avatar

Joe Tremblay jltrem

  • College Station, TX
View GitHub Profile
/// <summary>
/// Run an action after the specified delay
/// </summary>
/// <param name="delayMillis">millisecond delay before running an action</param>
/// <param name="doOnce">action to run</param>
public static void RunOneShot(int delayMillis, Action doOnce)
{
if (delayMillis <= 0) throw new ArgumentException("RunOneShot delayMillis argument must be greater than 0");
if (doOnce == null) throw new ArgumentException("RunOneShot doOnce argument cannot be null");
static class EntityHelper
{
/// <summary>
/// Makes a shallow copy of an entity object. This works much like a MemberwiseClone()
/// but directly instantiates a new object and copies only properties that work with
/// EF and don't have the NotMappedAttribute.
///
/// ** It also avoids copying the EF's proxy reference that would occur by using MemberwiseClone() **
///
@jltrem
jltrem / ValidateEmailAddress.cs
Created August 3, 2014 01:30
validate an email address
string regex = @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z";
bool isEmail = Regex.IsMatch(emailString, regex, RegexOptions.IgnoreCase);
@jltrem
jltrem / CopyProperties.cs
Last active August 29, 2015 14:04
method to create an object and copy the properties from another object into it
// expected usage: var tableRec = Utils.CopyProperties<StatDbRecord>(fileRow, typeof(IStat));
static public class Utils
{
static public TDst CopyProperties<TDst>(object src, Type hasProps) where TDst : class, new()
{
var dst = new TDst();
foreach (var propInf in hasProps.GetProperties())
{
var val = propInf.GetValue(src, null);
@jltrem
jltrem / TestMethodWasCalled.cs
Last active August 29, 2015 14:05
checking that a method was called with xunit & moq
using System;
using Xunit;
using Moq;
public class Program
{
static void Main(string[] args)
{
}
@jltrem
jltrem / TestUtil.cs
Created August 12, 2014 21:43
get random strings for use in testing
public static class TestUtil
{
private static Random _random = new Random();
public static string MakeRandomString(int numchars = 8)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
return new string(Enumerable.Repeat(chars, numchars).Select(x => x[_random.Next(x.Length)]).ToArray());
}
Regex.Match(fullstring, @"\b" + findstr + @"\b", RegexOptions.IgnoreCase).Success
public static bool IsNumeric(this string str)
{
decimal dec;
bool numeric = decimal.TryParse(str, out dec);
if (!numeric)
{
// this will handle scientific notation (e.g., "314e-2")
numeric = decimal.TryParse(str, System.Globalization.NumberStyles.Float, null, out dec);
}
return numeric;
// Return the text found between two other substrings.
public static string Substring(this string src, string startStr, string finishStr)
{
string substr = "";
int atStart = src.IndexOf(startStr);
if (atStart >= 0)
{
int afterStart = atStart + startStr.Length;
int atFinish = src.IndexOf(finishStr, afterStart);
@jltrem
jltrem / DictionaryXmlSerialization.cs
Last active August 29, 2015 14:05
serializing IDictionary via XmlSerializer
/// <summary>
/// System.Collections.Generic.KeyValuePair has not setters so we have to roll our own in order to serialize.
/// </summary>
[Serializable]
[XmlType(TypeName = "KVP")]
public struct SerializableKeyValuePair<K, V>
{
public K Key { get; set; }
public V Value { get; set; }
}