Skip to content

Instantly share code, notes, and snippets.

@sebingel
sebingel / TSQL-to-POCO
Created February 19, 2019 16:50 — forked from joey-qc/TSQL-to-POCO
A simple TSQL script to quickly generate c# POCO classes from SQL Server tables and views. You may tweak the output as needed. Not all datatypes are represented but this should save a bunch of boilerplate coding. USAGE: Run this query against the database of your choice. The script will loop through tables, views and their respective columns. Re…
declare @tableName varchar(200)
declare @columnName varchar(200)
declare @nullable varchar(50)
declare @datatype varchar(50)
declare @maxlen int
declare @sType varchar(50)
declare @sProperty varchar(200)
DECLARE table_cursor CURSOR FOR
@sebingel
sebingel / Virtual.cs
Created February 3, 2018 22:37
Virtual in C#
using System;
namespace ConsoleApplication1
{
internal class A
{
public virtual void Foo()
{
Console.WriteLine("Foo A");
}
@sebingel
sebingel / HelloWorld.cs
Created February 3, 2018 22:36
Creating a Hello World
// https://social.msdn.microsoft.com/Forums/en-US/3a720901-fdc5-47dc-8057-e06454525c82/how-to-generate-c-code-at-run-time-using-c-language?forum=csharplanguage
// https://benohead.com/three-options-to-dynamically-execute-csharp-code/
// http://stackoverflow.com/questions/875723/how-to-debug-break-in-codedom-compiled-code
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Globalization;
using System.IO;
using System.Reflection;
using (var md5 = System.Security.Cryptography.MD5.Create())
{
using (var stream = File.OpenRead(@"E:\somefile.zip"))
{
Console.WriteLine(BitConverter.ToString(md5.ComputeHash(stream)).Replace("-","‌​").ToLower());
}
}
@sebingel
sebingel / Program.cs
Created August 29, 2017 15:23
Short example of MemoryCache
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
namespace CacheTest
{
internal class Program
{
private UnitRepo _unitrepo;
@sebingel
sebingel / CalculateVectorCircleIntersection.cs
Created September 12, 2016 11:51
Calculates the intersection of a vector (given by two points) with a circle (point and radius)
using System;
using System.Drawing;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main()
{
Point start = new Point(0, 0);
@sebingel
sebingel / StreamEquals.cs
Last active March 16, 2021 22:28
Compare two streams bit by bit in C#
private bool StreamEquals(Stream a, Stream b)
{
if (a == b)
{
return true;
}
if (a == null || b == null)
{
throw new ArgumentNullException(a == null ? "a" : "b");
How to detect the execution platform ?
The execution platform can be detected by using the System.Environment.OSVersion.Platform value. However correctly detecting Unix platforms, in every cases, requires a little more work. The first versions of the framework (1.0 and 1.1) didn't include any PlatformID value for Unix, so Mono used the value 128. The newer framework 2.0 added Unix to the PlatformID enum but, sadly, with a different value: 4 and newer versions of .NET distinguished between Unix and MacOS X, introducing yet another value 6 for MacOS X.
This means that in order to detect properly code running on Unix platforms you must check the three values (4, 6 and 128). This ensure that the detection code will work as expected when executed on Mono CLR 1.x runtime and with both Mono and Microsoft CLR 2.x runtimes.
@sebingel
sebingel / OsName.cs
Created February 2, 2016 21:10
Gets the friendly OS Name in C#
string name = (from x in new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem").Get().Cast<ManagementObject>()
select x.GetPropertyValue("Caption")).FirstOrDefault();
System.Console.WriteLine(name != null ? name.ToString() : "Unknown");
@sebingel
sebingel / ParallelComparison.cs
Last active January 27, 2016 09:26
A small application to demonstrate different implementations of parallel computation in c# featuring await async pattern, BackgroundWorker and Task.
using System;
using System.ComponentModel;
using System.IO;
using System.Threading.Tasks;
namespace sebingel.ParallelComparision
{
internal class Program
{
static void Main()