December 2010 - Posts
For those of you still using Visual Studio 2008 and SharePoint 2007, Microsoft has finally released the long awaited version 1.3 of the integration extensions. Read the notice here and get it from here.
How to implement basic logical operations (AND, OR, NOT, XOR) with LINQ expressions:
public static class ExpressionExtensions
{
public static Expression<Func<T, Boolean>> Or<T>(this Expression<Func<T, Boolean>> expr, Expression<Func<T, Boolean>> other)
{
InvocationExpression invokedExpr = Expression.Invoke(other, expr.Parameters.Cast<Expression>());
return (Expression.Lambda<Func<T, Boolean>>(Expression.OrElse(expr.Body, invokedExpr), expr.Parameters));
}
public static Expression<Func<T, Boolean>> Xor<T>(this Expression<Func<T, Boolean>> expr, Expression<Func<T, Boolean>> other)
{
InvocationExpression invokedExpr = Expression.Invoke(other, expr.Parameters.Cast<Expression>());
return (Expression.Lambda<Func<T, Boolean>>(Expression.ExclusiveOr(expr.Body, invokedExpr), expr.Parameters));
}
public static Expression<Func<T, Boolean>> Not<T>(this Expression<Func<T, Boolean>> expr)
{
return (Expression.Lambda<Func<T, Boolean>>(Expression.Not(expr.Body), expr.Parameters));
}
public static Expression<Func<T, Boolean>> And<T>(this Expression<Func<T, Boolean>> expr, Expression<Func<T, Boolean>> other)
{
InvocationExpression invokedExpr = Expression.Invoke(other, expr.Parameters.Cast<Expression>());
return (Expression.Lambda<Func<T, Boolean>>(Expression.AndAlso(expr.Body, invokedExpr), expr.Parameters));
}
}
In .NET 4.0 the policy APIs have changed a bit. Here's how you can create a sandboxed instance of a type, which must inherit from MarshalByRefObject:
static T CreateRestrictedType<T>(SecurityZone zone, params Assembly [] fullTrustAssemblies) where T : MarshalByRefObject, new()
{
return(CreateRestrictedType<T>(zone, fullTrustAssemblies, new IPermission [0]);
}
static T CreateRestrictedType<T>(SecurityZone zone, params IPermission [] additionalPermissions) where T : MarshalByRefObject, new()
{
return(CreateRestrictedType<T>(zone, new Assembly [0], additionalPermissions);
}
static T CreateRestrictedType<T>(SecurityZone zone, Assembly [] fullTrustAssemblies, IPermission [] additionalPermissions) where T : MarshalByRefObject, new()
{
Evidence evidence = new Evidence();
evidence.AddHostEvidence(new Zone(zone));
PermissionSet evidencePermissionSet = SecurityManager.GetStandardSandbox(evidence);
foreach (IPermission permission in additionalPermissions ?? new IPermission[ 0 ])
{
evidencePermissionSet.AddPermission(permission);
}
StrongName [] strongNames = (fullTrustAssemblies ?? new Assembly[0]).Select(a => a.Evidence.GetHostEvidence<StrongName>()).ToArray();
AppDomainSetup adSetup = new AppDomainSetup();
adSetup.ApplicationBase = Path.GetDirectoryName(typeof(T).Assembly.Location);
AppDomain newDomain = AppDomain.CreateDomain("Sandbox", evidence, adSetup, evidencePermissionSet, strongNames);
ObjectHandle handle = Activator.CreateInstanceFrom(newDomain, typeof(T).Assembly.ManifestModule.FullyQualifiedName, typeof(T).FullName);
return (handle.Unwrap() as T);
}
Finally!
Read about it here and download it here.
A pooled lifetime manager for Unity. Creates new objects up to a maximum size and returns them in round-robin sequence. Default pool size is set to 5, but you can override it in the appSettings section with a key:
<appSettings>
<!-- format is TypeName + "PoolSize" --/>
<add key="ISomeTypePoolSize" value="10" />
</appSettings>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<container>
<register type="ISomeType" mapTo="SomeType">
<lifetime type="PooledLifetimeManager`1[[ISomeType]]"/>
</register>
</container>
</unity>
Here's the code:
public class PooledLifetimeManager<T> : SynchronizedLifetimeManager where T : class
{
private static List<T> list = null;
private static Int32 index = -1;
static PooledLifetimeManager()
{
Int32 capacity = 5;
Int32.TryParse(ConfigurationManager.AppSettings [ String.Concat(typeof(T).Name, "PoolSize") ] ?? capacity.ToString(), out capacity);
list = new List<T>(capacity);
}
protected override Object SynchronizedGetValue()
{
if (list.Capacity > list.Count)
{
return (null);
}
else
{
++index;
if (index == list.Capacity)
{
index = 0;
}
return (list [ index ]);
}
}
protected override void SynchronizedSetValue(Object newValue)
{
if (list.Count < list.Capacity)
{
list.Add(newValue as T);
}
}
}
More Posts