Skip to content

Instantly share code, notes, and snippets.

@mahmut-gundogdu
Last active October 17, 2016 12:52
Show Gist options
  • Save mahmut-gundogdu/9de2d6b50f3afa490798 to your computer and use it in GitHub Desktop.
Save mahmut-gundogdu/9de2d6b50f3afa490798 to your computer and use it in GitHub Desktop.
En Çok Kullandığım işlevleri Extensiyon yapıyorum. Böylece her projede kullanırken ufak da olsa zaman kazanıyorum. Kullanmak istediğiniz projeye eklemeniz yeteril.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.SqlClient;
using System.IO;
public static class MGExtensions
{
public static bool DoluMu(this string s)
{
return string.IsNullOrEmpty(s) == false;
}
public static int ToInt(this string s)
{
return Convert.ToInt32(s);
}
public static int? ToIntNullable(this object obj)
{
if (obj != null)
{
string s = obj.ToString();
int sonuc = 0;
if (int.TryParse(s, out sonuc))
{
return sonuc;
}
else
return null;
}
else
return null;
}
public static string DomainAdi(this Uri url)
{
string baseUrl = url.GetLeftPart(UriPartial.Authority);
return baseUrl;
}
public static string GuidFileName(this string s)
{
var uzanti = Path.GetExtension(s);
string dosyaAdi = Guid.NewGuid().ToString();
return dosyaAdi + uzanti;
}
public static ObservableCollection<T> ToObservableCollection<T>
(this IEnumerable<T> source)
{
return new ObservableCollection<T>(source);
}
/// <summary>
/// ~/resimler/dosyaadi.jpg gibi verilen resmi siler
/// </summary>
/// <param name="dosyaAdi"></param>
public static bool DosyaSil(string dosyaAdi)
{
var fullPath = HttpContext.Current.Server.MapPath(dosyaAdi);
if (System.IO.File.Exists(fullPath))
{
System.IO.File.Delete(fullPath);
return true;
}
return false;
}
private static bool IsNullableType(Type theValueType)
{
return (theValueType.IsGenericType && theValueType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)));
}
/// <summary>
/// Returns the value, of type T, from the SqlDataReader, accounting for both generic and non-generic types.
/// </summary>
/// <typeparam name="T">T, type applied</typeparam>
/// <param name="theReader">The SqlDataReader object that queried the database</param>
/// <param name="theColumnName">The column of data to retrieve a value from</param>
/// <returns>T, type applied; default value of type if database value is null</returns>
public static T Deger<T>(this SqlDataReader theReader, string theColumnName)
{
// Read the value out of the reader by string (column name); returns object
object theValue = theReader[theColumnName];
// Cast to the generic type applied to this method (i.e. int?)
Type theValueType = typeof(T);
// Check for null value from the database
if (DBNull.Value != theValue)
{
// We have a null, do we have a nullable type for T?
if (!IsNullableType(theValueType))
{
// No, this is not a nullable type so just change the value's type from object to T
return (T)Convert.ChangeType(theValue, theValueType);
}
else
{
// Yes, this is a nullable type so change the value's type from object to the underlying type of T
NullableConverter theNullableConverter = new NullableConverter(theValueType);
return (T)Convert.ChangeType(theValue, theNullableConverter.UnderlyingType);
}
}
// The value was null in the database, so return the default value for T; this will vary based on what T is (i.e. int has a default of 0)
return default(T);
}
#region bu kod http://stackoverflow.com/questions/17611925/string-format-extension-method dan alıntıdır.
// Kullanımı new {Person = "Me", Until = new DateTime(2013,8,1)}
// .Format("{Person} is away until {Until:yyyy-MM-dd});
public static string Format(this object data, string format)
{
var values = new List<object>();
var type = data.GetType();
format = Regex.Replace(format, @"(^|[^{])\{([^{}]+)\}([^}]|$)", x =>
{
var keyValues = Regex.Split(x.Groups[2].Value,
"^([^:]+):?(.*)$")
.Where(y => !string.IsNullOrEmpty(y));
var key = keyValues.ElementAt(0);
var valueFormat = keyValues.Count() > 1 ?
":" + keyValues.ElementAt(1) :
string.Empty;
var value = GetValue(key, data, type);
values.Add(value);
return string.Format("{0}{{{1}{2}}}{3}",
x.Groups[1].Value,
values.Count - 1,
valueFormat,
x.Groups[3].Value);
});
return string.Format(format, values.ToArray());
}
private static object GetValue(string name, object data, Type type)
{
var info = type.GetProperty(name);
return info.GetValue(data, new object[0]);
}
#endregion
// DAN ALINTI http://stackoverflow.com/questions/ 16612080/how-would-i-write-a-string-format-extension-method
//KULLANIMI: var ask = "Continue using [{0}]?".FormatWith(value);
public static string FormatWith(this string value, params object[] args)
{
return String.Format(value, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment