Skip to content

Instantly share code, notes, and snippets.

View zHaytam's full-sized avatar
🎯
Focusing

Haytam Zanid zHaytam

🎯
Focusing
View GitHub Profile
public class DiagramOptions
{
[Description("Key code for deleting entities")]
public string DeleteKey { get; set; } = "Delete";
[Description("Whether to inverse the zoom direction or not")]
public bool InverseZoom { get; set; }
[Description("The default component for nodes")]
public Type? DefaultNodeComponent { get; set; }
[Description("The grid size (grid-based snaping")]
public int? GridSize { get; set; }
public static class ReflectionUtils
{
public static IEnumerable<PossibleOption> ExtractPossibleOptions<T>()
{
var type = typeof(T);
return ExtractPossibleOptions(type, string.Empty, Activator.CreateInstance(type));
}
private static IEnumerable<PossibleOption> ExtractPossibleOptions(Type type, string prefix, object instance)
{
public class PossibleOption
{
public string Name { get; }
public string Type { get; }
public string Default { get; }
public string Description { get; }
public PossibleOption(string name, string type, string @default, string description)
{
Name = name;
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
public class ListProductsRequest
{
public string CategoryId { get; set; }
public int? Page { get; set; }
public int? Size { get; set; }
public string Filter { get; set; }
}
[
{
"name": "categoryId",
"value": "qgq65s1d6qs5d1g"
},
{
"name": "page",
"value": 1
},
{
var instance = new ListProductsRequest
{
CategoryId = "qdfg3514sqdf65",
Page = 1,
Size = 10,
Filter = "price>10"
};
var value = instance.CategoryId;
object instance = new ListProductsRequest
{
CategoryId = "qdfg3514sqdf65",
Page = 1,
Size = 10,
Filter = "price>10"
};
var value = (instance as ListProductsRequest).CategoryId;
public string GetCategoryId(object instance)
=> (instance as ListProductsRequest).CategoryId;
// Or as a lambda expression (which will help us more)
Func<object, string> getCategoryId = (instance) => (instance as ListProductsRequest).CategoryId;
// We can also generalize it
Func<object, object> getCategoryId = (instance) => (object)((instance as ListProductsRequest).CategoryId);
private static Func<object, object> GenerateGetterLambda(PropertyInfo property)
{
// Define our instance parameter, which will be the input of the Func
var objParameterExpr = Expression.Parameter(typeof(object), "instance");
// 1. Cast the instance to the correct type
var instanceExpr = Expression.TypeAs(objParameterExpr, property.DeclaringType);
// 2. Call the getter and retrieve the value of the property
var propertyExpr = Expression.Property(instanceExpr, property);
// 3. Convert the property's value to object
var propertyObjExpr = Expression.Convert(propertyExpr, typeof(object));