Skip to content

Instantly share code, notes, and snippets.

//using the following interfaces
public interface IUseInterface<T> where T : IAmAnInterface { T ViewModel { get; set; } }
private Type GetGenericType<TInterface>(TInterface someObjectInterface) where TInterface : IAmAnInterface =>
typeof(IUseInterface<>).MakeGenericType(someObjectInterface.GetType().GetInterfaces()
.Where(x => x.Name.Contains("Template")).ToArray());
//returns Type IUseInterface<T> where T : IAmAnInterface
@ABaker86
ABaker86 / Builder_FuncToReturnThis.cs
Created January 16, 2020 01:43
Using Func<T> to keep code DRY
Func<Action, ModelBuilder> ReturnBuilderAfter => a => { a(); return this; };
public ModelBuilder AddItem<T>(string name, T value) => ReturnBuilderAfter(() => {
_Model.Items.Add(new ModelItem() {
Name = name,
Value = value.ToString(),
ItemType = value.GetType().ToString()
});
});
@ABaker86
ABaker86 / WinForm_ActionToUpdate.cs
Created January 16, 2020 01:35
Update control using Action to keep code DRY
Action<Label, string, Point, Font> Up => (l, s, p, f) =>
{
l.Text = s;
l.Size = new Size(p);
l.Font = f;
};
void UpdateLblOne() => Up(lblOne, TEXT, SIZE, FONT);
void UpdateLblTwo() => Up(label2, TEXT, SIZE, FONT);