Skip to content

Instantly share code, notes, and snippets.

@wi7a1ian
wi7a1ian / Impersonator.cs
Last active July 19, 2019 09:25
Code snippet for impersonating current process #impersonate #csharp
internal class Impersonator : IDisposable
{
private static string UserName => "Foo";
private static string Password => "Bar";
private readonly SafeTokenHandle handle;
private readonly WindowsIdentity identity;
public Impersonator()
{
@wi7a1ian
wi7a1ian / IDialogService.cs
Last active March 30, 2020 17:56
Basic dialog/modal service #mvvm #wpf #csharp
public interface IDialogViewModel
{
event EventHandler Closed;
void Close();
}
public class DialogRequestedEventArgs : EventArgs
{
public FrameworkElement View { get; set; }
public IDialogViewModel ViewModel { get; set; }
@wi7a1ian
wi7a1ian / ModalPresenterControl.xaml
Last active May 20, 2019 10:13
UserControl designed as modal presenter for the main window #wpf #csharp
<UserControl ...>
<UserControl.Resources>
<c:NullToVisibilityConverter x:Key="NullToVisibilityConverter" />
<c:NullToBoolConverter x:Key="NullToBoolConverter" />
<Style x:Key="ModalVisibility" TargetType="FrameworkElement">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding ModalToShow, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Converter={StaticResource NullToBoolConverter}}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
@wi7a1ian
wi7a1ian / ViewModelBase.cs
Created May 20, 2019 10:05
Tiny (helper) base class for viewmodels in #wpf #csharp
public class ViewModelBase : BindableBase
{
private readonly static DependencyObject dummy = new DependencyObject();
public static bool IsInDesignMode() => DesignerProperties.GetIsInDesignMode(dummy);
public static Dispatcher UIDispatcher => (dummy.Dispatcher != null && dummy.Dispatcher.Thread.IsAlive) ? dummy.Dispatcher : null;
public static bool TryDispatch(Action action)
{
@wi7a1ian
wi7a1ian / global-exception-handlers.cs
Created May 20, 2019 09:56
Global exception handlers for #wpf #csharp
Current.DispatcherUnhandledException += (sender, e) =>
{
// log unhandled exceptions thrown from within main UI thread
e.Handled = true; // Prevent default unhandled exception processing
};
AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
{
// log unhandled exceptions thrown from background threads
};
@wi7a1ian
wi7a1ian / oop-design-patterns.md
Last active February 1, 2024 18:06
Design patterns in object-oriented programming

Any fool can write code that a computer can understand. Good programmers write code that humans can understand. - Martin Fowler

Legend:

  • 🔌 promotes decoupled architecture
  • 🎓 synergy with SOLID principles (ocp, srp, dip...)
  • ▶️ runtime focused (i.e: promotes reconfiguration)
  • 🛠 compiletime focused
  • 📝 configurable once (during initialization, usually from a config file)
  • 🆕 creational pattern
  • ⛓ structural pattern
@wi7a1ian
wi7a1ian / FormulaBinding.cs
Last active July 19, 2019 09:26
FormulaBinding is just a MultiBinding that uses FormulaConverter to convert formulas #csharp #wpf
public class FormulaBinding : MultiBinding
{
public FormulaBinding()
{
Converter = new FormulaConverter();
Mode = BindingMode.OneWay;
}
public string Formula
{
@wi7a1ian
wi7a1ian / cpp-paradigm-async.hpp
Last active September 16, 2019 10:10
Examples of programming paradigms in C++ #cpp
// TODO: asynchronous
@wi7a1ian
wi7a1ian / IsAnonymousType.cs
Last active March 28, 2019 13:15
The only way to detect anonymous types right now #csharp
private bool IsAnonymousType(Type type)
{
if (type == null)
throw new ArgumentNullException("type");
// HACK: The only way to detect anonymous types right now.
return Attribute.IsDefined(type, typeof(CompilerGeneratedAttribute), false)
&& type.IsGenericType && type.Name.Contains("AnonymousType")
&& (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$"))
&& (type.Attributes & TypeAttributes.NotPublic) == TypeAttributes.NotPublic;
@wi7a1ian
wi7a1ian / DirectoryHelper.cs
Created March 28, 2019 13:01
Makes file lookup easier #csharp
public static IEnumerable<string> GetUnknownFiles(string path)
{
return Directory.GetFiles(path, "*", SearchOption.AllDirectories).Where(filePath => string.IsNullOrWhiteSpace(Path.GetExtension(filePath)));
}
public static IEnumerable<string> GetFilesWithExtensions(string path)
{
return Directory.GetFiles(path, "*", SearchOption.AllDirectories).Where(filePath => !string.IsNullOrWhiteSpace(Path.GetExtension(filePath)));
}