Skip to content

Instantly share code, notes, and snippets.

@jbe2277
jbe2277 / DiscoveryService.cs
Last active September 4, 2021 10:50
[Xamarin, MAUI] Parse IPAddress provided by iOS NetServiceBrowser (supports IPv4 and IPv6)
private void ServiceAddressResolved(object sender, EventArgs e)
{
var service = (NSNetService)sender;
var addresses = service.Addresses.Select(x => x.ToArray()).ToArray();
var ipAddress = GetIPAddress(addresses.First()); // Consider that resolve might provide multiple IP addresses
// ...
}
private static IPAddress? GetIPAddress(byte[] data)
{
@jbe2277
jbe2277 / SyndicationClient.cs
Last active May 27, 2019 19:55
Simple Atom feed reader implementation
internal class SyndicationClient : ISyndicationClient
{
private const string XmlnsAtom2005 = "http://www.w3.org/2005/Atom";
public async Task<FeedDto> RetrieveFeedAsync(Uri uri)
{
XDocument doc;
using (var client = new HttpClient())
using (var stream = await client.GetStreamAsync(uri).ConfigureAwait(false))
{
@jbe2277
jbe2277 / WindowsFirewallService.cs
Created February 11, 2019 20:32
Windows Firewall: Read and Add Rule
internal class WindowsFirewallService
{
public static void AddRule(string ruleName, string udpPort)
{
var policy = CreatePolicy();
var rule = CreateRule();
rule.Name = ruleName;
rule.Enabled = true;
rule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
rule.Profiles = (int)NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_ALL;
@jbe2277
jbe2277 / WakeOnLan.cs
Created October 15, 2017 17:59
Wake On Lan command line application
using System;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
namespace WakeOnLan
{
internal class Program
{
@jbe2277
jbe2277 / Mef2ToMef1ReflectionContext.cs
Created July 16, 2017 18:09
Reflection Context that maps MEF2 (NuGet 'System.Composition') attributes to MEF1 (.NET 4.x 'System.ComponentModel.Composition') attributes. This class enables an application hosting MEF1 to use libraries which export their parts via MEF2 attributes.
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Reflection;
using System.Reflection.Context;
namespace WafApplication
{
// .NET 4.x Reference:
@jbe2277
jbe2277 / AssemblyAnalyzer.cs
Created July 2, 2017 17:52
AssemblyAnalyzer via System.Reflection.Metadata
using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
@jbe2277
jbe2277 / EquatableHelper.cs
Last active July 13, 2018 17:58
Automatic Equals and GetHashCode of Properties
internal static class EquatableHelper
{
private static readonly ConcurrentDictionary<Type, Func<object, object, bool>> getEqualsFunctions
= new ConcurrentDictionary<Type, Func<object, object, bool>>();
private static readonly ConcurrentDictionary<Type, Func<object, int>> getHashCodeFunctions
= new ConcurrentDictionary<Type, Func<object, int>>();
public static bool PropertiesEquals(object x, object y)
{
if (ReferenceEquals(x, y)) { return true; }
@jbe2277
jbe2277 / OptimizeSqlFile.cs
Created May 4, 2017 20:38
Convert SQL Compact Edition database file to SQLite database file
// === Convert SQL Compact Edition database file to SQLite database file ===
// Dump SQL Compact Edition data to SQLite compatible SQL file
// - Command line tools: https://github.com/ErikEJ/SqlCeToolbox/wiki/Command-line-tools
// - ExportSQLCE40.exe "Data Source=D:\Northwind.sdf;" Northwind.sql sqlite
// The SQL file created by the SqlCeToolbox contains GUID data as text.
// - Connection string with GUID as text: https://www.connectionstrings.com/sqlite/
// Data Source=c:\mydb.db;Version=3;BinaryGUID=False;
// - Recommended is to store GUID data as binary instead of text (requires less space)