Skip to content

Instantly share code, notes, and snippets.

/// <summary>
/// Custom function to perform looping operation.
/// </summary>
/// <typeparam name="T">type of ienumerable</typeparam>
/// <param name="list">The list.</param>
/// <param name="action">The action.</param>
/// <returns>index value</returns>
/// <exception cref="System.ArgumentNullException"> if the supplied action is null</exception>
public static int ForEach<T>(this IEnumerable<T> list, Action<int, T> action)
{
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
namespace Shared.ApplicationLoaderUtil
{
/// <summary>
@vendettamit
vendettamit / reflection.cs
Created May 24, 2013 13:34
Reflection miscelleneous
/// <summary>
/// Gets the error message attribute.
/// </summary>
/// <param name="memberName">Name of the member.</param>
/// <returns>
/// value of attribute from class
/// </returns>
public static string GetErrorMessageAttributeValue(string memberName)
{
string message = string.Empty;
@vendettamit
vendettamit / EventHandlerFetcher.cs
Created June 6, 2013 14:33
Check if the event is hooked with the Handler
public static void Usage()
{
var instance = new Timer();
// check if the event is assigned a handler
if (GetEventHandler(instance, "Elapsed", "ElapsedEventHandler") == null)
{
throw new InvalidOperationException(string.Format("The Timer elapsed event must be assigned an event handler. Source: {0}", type.FullName));
}
1. Stub() defines the behavior for stubbed object.
2. Expect() defines the behavior and the expectation for mocked object.
3. Follow the principle of "Test only one thing per test".
4. mockrepositoryObject doesn't stop recording until you call ReplayAll. Try using repositoryInstance.PlayBack();
sample:
using (repository.Playback())
{
}
5. Call repository.VerfiyAll() in the end.
@vendettamit
vendettamit / PartialDownloadController.cs
Last active August 24, 2020 14:22
WebApi controller to handle the single partial request for content
using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Web;
using System.Web.Http;
@vendettamit
vendettamit / gist:5774450
Created June 13, 2013 15:07
Http client Downloader for Retry(Palmer lib)/Resume download requests enabled.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
@vendettamit
vendettamit / AsyncMethodTemplate.cs
Created June 13, 2013 15:13
Async delegate template using tasks
/// <summary>
/// DoAsync is a method template to perform any task asynchronously.
/// </summary>
/// <typeparam name="T">type of argument</typeparam>
/// <param name="action">method</param>
/// <param name="argument">value of argument of type T</param>
public static void DoAsync<T>(Action<T> action, T argument)
{
// Invoke the task to execute the item
Task.Factory.StartNew(() => action.Invoke(argument),
@vendettamit
vendettamit / regex collections.cs
Created June 17, 2013 15:30
My Regex Collections
Testing websites:
http://www.regex101.com/r/yM3vL0
http://www.regexplanet.com/advanced/dotnet/index.html
UrlSourceOrLocalDriveWithRegex = @"(?i)\b^(https?|s?ftp)+:\/\/[aA-zZ]*[0-9]*|([aA-zZ]{1}:{1}\\{1}\b).*$";
Matches: Https://www.something.com, Ftp://somesource.com, sftp://securesource.com, http://moreurl.com, C:\path etc.
Unmatch: httpss://something.com, abcd://, c:, C,
SystemDriveOrUNCPathRegex = @"^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w ]*.*))+$"
Matches: c:\folder, \\server\folder
@vendettamit
vendettamit / tablemanipulator.js
Created July 29, 2013 12:32
Remove rowspan/colspan and split the cells with duplicate values
var tempTable = $('#tbl').clone(true);
var tableBody = $(tempTable).children();
$(tableBody).children().each(function(index , item){
var currentRow = item;
$(currentRow).children().each(function(index1, item1){
if($(item1).attr("rowspan"))
{
// copy the cell
var item2 = $(item1).clone(true);
// Remove rowspan