Skip to content

Instantly share code, notes, and snippets.

public static IEnumerable<byte[]> ReadChunks(string source)
{
using (Stream stream = IsUrl(source) ? WebRequest.Create(source).GetResponse().GetResponseStream() : new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read))
{
const int sampleSize = 300;
var fileSize = GetFileSize(source);
if (fileSize > sampleSize)
fileSize = sampleSize;
var buffer = new byte[fileSize];
if (stream == null)
public static object GetPropValue(object obj, string name)
{
foreach (var part in name.Split('.'))
{
if (obj == null) { return null; }
Type type = obj.GetType();
PropertyInfo info = type.GetProperty(part);
if (info == null) { return null; }
@lfgrando
lfgrando / MarkerAttributeExtension.cs
Last active July 20, 2018 18:16
Goldplated edition, works on MVC5, probably 4/3:filterContext.HasMarkerAttribute<RequireHttpsAttribute>()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace Core.Extensions
{
public static class MarkerAttributeExtension
{
public static bool HasMarkerAttribute<T>(this AuthorizationContext that)
public static string ToDescription<T>(this T source)
{
FieldInfo fi = source.GetType().GetField(source.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
//
return attributes.Length > 0 ? attributes[0].Description : source.ToString();
}
@lfgrando
lfgrando / RenameToken.cs
Last active July 20, 2018 18:16
Rename JObject Token
private void RenameToken(JToken token, string newName)
{
var parent = token.Parent;
if (parent == null)
throw new InvalidOperationException("The parent is missing.");
var newToken = new JProperty(newName, token);
parent.Replace(newToken);
}
@lfgrando
lfgrando / ServicebusMessageDefer.cs
Last active July 20, 2018 18:15
How to defer a ServiceBus message using custom properties.
public bool DoWork(BrokeredMessage message)
{
if (message == null)
throw new ArgumentNullException(nameof(message));
try
{
if (!_isInitialized)
Initialize();
@lfgrando
lfgrando / CsvRegex.cs
Last active February 27, 2021 10:59
CSV regex
(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)
/*
May be usefull to:
1997,Ford,E350,"ac, abs, moon",30100.00
1999,Chevy,"Venture ""Extended Edition""",,49000.00
1996,Jeep,Grand Cherokee,"MUST SELL!
air, moon roof, loaded",479699.00
*/
@lfgrando
lfgrando / FindJtokens.cs
Last active July 20, 2018 18:10
Find JTokens by name.
public List<JToken> FindTokens(JToken containerToken, string name)
{
List<JToken> matches = new List<JToken>();
FindTokens(containerToken, name, matches);
return matches;
}
private void FindTokens(JToken containerToken, string name, List<JToken> matches)
{
if (containerToken.Type == JTokenType.Array)
@lfgrando
lfgrando / ReplaceMatchToken.cs
Last active July 20, 2018 18:15
Replace custom matched token on JToken
public static class JTokenExtensions
{
private JToken LookupPreviousData(string value, JToken resourceToken)
{
if (!resourceToken.HasValues)
return null;
if (!Regex.Match(value, ProjectionMatch).Success)
return null;
@lfgrando
lfgrando / SetApiMaxContentLengthUpload.xml
Last active August 31, 2018 14:57
API Upload Limit Size to 1gb at Web.config. It seems it doesn't work at all IIS's. Looks like not working at IIS Express.
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>