using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Autofac;
using Autofac.Integration.Mvc;
using GreenTree.Nachtragsmanagement.Core.ComponentModel;
using GreenTree.Nachtragsmanagement.Core.Domain.User;
using GreenTree.Nachtragsmanagement.Core.Authentication;
namespace GreenTree.Nachtragsmanagement.Core
{
public static class CommonHelper
{
private static AspNetHostingPermissionLevel? _trustLevel;
///
/// Finds the trust level of the running application (http://blogs.msdn.com/dmitryr/archive/2007/01/23/finding-out-the-current-trust-level-in-asp-net.aspx)
///
/// The current trust level.
public static AspNetHostingPermissionLevel GetTrustLevel()
{
if (!_trustLevel.HasValue)
{
//set minimum
_trustLevel = AspNetHostingPermissionLevel.None;
//determine maximum
foreach (AspNetHostingPermissionLevel trustLevel in new[] {
AspNetHostingPermissionLevel.Unrestricted,
AspNetHostingPermissionLevel.High,
AspNetHostingPermissionLevel.Medium,
AspNetHostingPermissionLevel.Low,
AspNetHostingPermissionLevel.Minimal
})
{
try
{
new AspNetHostingPermission(trustLevel).Demand();
_trustLevel = trustLevel;
break; //we've set the highest permission we can
}
catch (System.Security.SecurityException)
{
continue;
}
}
}
return _trustLevel.Value;
}
#region Conversion
///
/// Sets a property on an object to a valuae.
///
/// The object whose property to set.
/// The name of the property to set.
/// The value to set the property to.
public static void SetProperty(object instance, string propertyName, object value)
{
if (instance == null) throw new ArgumentNullException("instance");
if (propertyName == null) throw new ArgumentNullException("propertyName");
Type instanceType = instance.GetType();
PropertyInfo pi = instanceType.GetProperty(propertyName);
if (pi == null)
throw new Exception(
String.Format("No property '{0}' found on the instance of type '{1}'.", propertyName, instanceType));
if (!pi.CanWrite)
throw new Exception(
String.Format("The property '{0}' on the instance of type '{1}' does not have a setter.", propertyName, instanceType));
if (value != null && !value.GetType().IsAssignableFrom(pi.PropertyType))
value = To(value, pi.PropertyType);
pi.SetValue(instance, value, new object[0]);
}
public static TypeConverter GetCustomTypeConverter(Type type)
{
//we can't use the following code in order to register our custom type descriptors
//TypeDescriptor.AddAttributes(typeof(List), new TypeConverterAttribute(typeof(GenericListTypeConverter)));
//so we do it manually here
if (type == typeof(List))
return new GenericListTypeConverter();
if (type == typeof(List))
return new GenericListTypeConverter();
if (type == typeof(List))
return new GenericListTypeConverter();
return TypeDescriptor.GetConverter(type);
}
///
/// Converts a value to a destination type.
///
/// The value to convert.
/// The type to convert the value to.
/// The converted value.
public static object To(object value, Type destinationType)
{
return To(value, destinationType, CultureInfo.InvariantCulture);
}
///
/// Converts a value to a destination type.
///
/// The value to convert.
/// The type to convert the value to.
/// Culture
/// The converted value.
public static object To(object value, Type destinationType, CultureInfo culture)
{
if (value != null)
{
var sourceType = value.GetType();
TypeConverter destinationConverter = GetCustomTypeConverter(destinationType);
TypeConverter sourceConverter = GetCustomTypeConverter(sourceType);
if (destinationConverter != null && destinationConverter.CanConvertFrom(value.GetType()))
return destinationConverter.ConvertFrom(null, culture, value);
if (sourceConverter != null && sourceConverter.CanConvertTo(destinationType))
return sourceConverter.ConvertTo(null, culture, value, destinationType);
if (destinationType.IsEnum && value is int)
return Enum.ToObject(destinationType, (int)value);
if (!destinationType.IsInstanceOfType(value))
return Convert.ChangeType(value, destinationType, culture);
}
return value;
}
///
/// Converts a value to a destination type.
///
/// The value to convert.
/// The type to convert the value to.
/// The converted value.
public static T To(object value)
{
//return (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
return (T)To(value, typeof(T));
}
///
/// Convert enum for front-end
///
/// Input string
/// Converted string
public static string ConvertEnum(string str)
{
string result = string.Empty;
char[] letters = str.ToCharArray();
foreach (char c in letters)
if (c.ToString() != c.ToString().ToLower())
result += " " + c.ToString();
else
result += c.ToString();
return result;
}
#endregion
#region User-Handling
///
/// Evaluate the user context of the current logged in user
///
public static UserContext UserContext()
{
var container = Singleton.Instance;
var userHelper = container.Resolve();
return new UserContext();
}
#endregion
}
}