Skip to content

Instantly share code, notes, and snippets.

View M-Yankov's full-sized avatar
💻
Driven by functionality, not by design

Mihail Yankov M-Yankov

💻
Driven by functionality, not by design
View GitHub Profile
@M-Yankov
M-Yankov / LoaderBootstrap.html
Created November 26, 2017 23:16
Width loader
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
<style>
@M-Yankov
M-Yankov / DynamicModel.cs
Created November 3, 2017 13:10
How to get all properties from a dynamic model.
dynamic person = new object { Name = "John", Age = 32, Title = "HR" };
// In case the person comes from somewhere else
if (person != null)
{
var properties = person.GetType().GetPtoperties(); // Will return an Array of System.Reflection.PropertyInfo[];
// or
Type theType = (Type)person.GetType();
@M-Yankov
M-Yankov / ViewDataBagModel.md
Created November 3, 2017 07:52
Differences between ViewBag, ViewData and model
  • With ViewBag (dynamic type):
    • Action: ViewBag.Message = "Hello World!";
    • View: @ViewBag.Message
  • With ViewData (dictionary)
    • Action: ViewData["message"] = "Hello World!";
    • View: @ViewData["message"]
  • With Strongly-typed views:
    • Action: return View(model);
    • View: @model ModelDataType;
@M-Yankov
M-Yankov / GetAsyncResult.cs
Created October 27, 2017 14:12
Returns a result from Task
string text = System.Threading.Tasks.Task.Run(() => "TEXT".ToLower()).GetAwaiter().GetResult();
// To retreive the result it bloks the other code.
// https://www.youtube.com/watch?v=fX62PJ_wv20
@M-Yankov
M-Yankov / AsyncCode.cs
Created October 27, 2017 13:58
How to execute something asynchronous
var taskArray = new Task[tasksCount];
for (int index = 0; index < taskArray.Length; index++)
{
var productId = productIds[index];
var task = Task.Run(() => UpdatePoductStatus(productId, Status.Available));
taskArray[index] = task;
}
Task.WaitAll(taskArray);
@M-Yankov
M-Yankov / PasteXMLasCSharpClasses.md
Last active October 20, 2017 11:48
An instructions to add XML structure as C# code

How to add an XML structered code to C# classes.

  1. copy a valid XML code
<items>
  <item>
    <title>
      Lorem ipsum 2017-10-19T13:55:00+00:00
 
@M-Yankov
M-Yankov / FixUtcDateOnClient.js
Created October 19, 2017 15:04
Fxing the UTC dates on client side
/*
This script will work only on the client-side
*/
function toClientDate(dateText) {
if (!validateInput(dateText)) {
return '';
}
var date = new Date(dateText);
@M-Yankov
M-Yankov / TernaryOperatorReadable.cs
Created October 12, 2017 19:59
It seems that the following syntax for nested ternary operators is the most readable (for me);
return
<First_Expression> ? "Value1" :
<Second_Expression> ? "Value2" :
<Third_Expression> ? "Value3":
"Value4"; // And so on....
@M-Yankov
M-Yankov / EventAddRemove.cs
Created October 11, 2017 13:34
Event property has add/remove instead of get/set
public event EventHandler AuthenticateRequest
{
add
{
this.AddSyncEventHookup(HttpApplication.EventAuthenticateRequest, value, RequestNotification.AuthenticateRequest);
}
remove
{
this.RemoveSyncEventHookup(HttpApplication.EventAuthenticateRequest, value, RequestNotification.AuthenticateRequest);
}
@M-Yankov
M-Yankov / AttibutesOnGetSet.cs
Created October 11, 2017 12:01
In C# you can set an attribute to the setter. It works in normal .NET 4.5
// Extracted from System.Web
public IPrincipal User
{
get
{
return this._principalContainer.Principal;
}
[SecurityPermission(SecurityAction.Demand, ControlPrincipal=true)]
set
{