var answer1 = GetSomeStrings().Cast<MyType>(); try { foreach (var v in answer1) Console.WriteLine(v); } catch (InvalidCastException) { Console.WriteLine("Cast Failed!"); }
var answer2 = from MyType v in GetSomeStrings() select v; try { foreach (var v in answer2) Console.WriteLine(v); } catch (InvalidCastException) { Console.WriteLine("Cast failed again"); }
public class Product { public decimal Cost { get; set; } public string Name { get; set; } public decimal Price { get { return Cost * 1.15M; } } }
// Derived Product class: public class SpecialProduct : Product { public string ReasonOnSpecial { get; set; } // other methods elided }
// elsewhere public static void WritePricingInformation(dynamic product) { Console.WriteLine("The price of one {0} is {1}", product.Name, product.Price); }
public static void WritePricingInformation(Product product) { Console.WriteLine("In type safe version"); Console.WriteLine("The price of one {0} is {1}",
product.Name, product.Price);
}
public TResult CallInterface<TResult>(Expression< Func<T, TResult>> op) { var exp = op.Body as MethodCallExpression; var methodName = exp.Method.Name; var methodInfo = exp.Method; var allParameters = from element in exp.Arguments select processArgument(element); Console.WriteLine("Calling {0}", methodName); foreach (var parm in allParameters) Console.WriteLine( "\tParameter type = {0}, Value = {1}", parm.Item1, parm.Item2); return default(TResult); }
private void createConverterIfNeeded() { if (converter == null) { var source = Expression.Parameter(typeof(TSource), "source"); var dest = Expression.Variable(typeof(TDest), "dest"); var assignments = from srcProp in typeof(TSource).GetProperties( BindingFlags.Public | BindingFlags.Instance) where srcProp.CanRead let destProp = typeof(TDest). GetProperty( srcProp.Name, BindingFlags.Public | BindingFlags.Instance) where (destProp != null) && (destProp.CanWrite) select Expression.Assign( Expression.Property(dest, destProp), Expression.Property(source, srcProp)); // put together the body: var body = new List<Expression>(); body.Add(Expression.Assign(dest, Expression.New(typeof(TDest)))); body.AddRange(assignments); body.Add(dest); var expr = Expression.Lambda<Func<TSource, TDest>>( Expression.Block( new[] { dest }, // expression parameters body.ToArray() // body ), source // lambda expression ); var func = expr.Compile(); converter = func; } }
public class MemoryMonitor : INotifyPropertyChanged, INotifyPropertyChanging { System.Threading.Timer updater; public MemoryMonitor() { updater = new System.Threading.Timer((_) => timerCallback(_), null, 0, 5000); } private void timerCallback(object unused) { UsedMemory = GC.GetTotalMemory(false); } public long UsedMemory { get { return mem; } private set { if (value != mem) { if (PropertyChanging != null) PropertyChanging(this, new PropertyChangingEventArgs( "UsedMemory")); mem = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs( "UsedMemory")); } } } private long mem; #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; #endregion #region INotifyPropertyChanging Members public event PropertyChangingEventHandler PropertyChanging; #endregion }
public static class PropertyNotifyExtensions { public static T SetNotifyProperty<T>(this PropertyChangedEventHandler handler, T newValue, Expression<Func<T>> oldValueExpression, Action<T> setter) { return SetNotifyProperty(handler, null, newValue, oldValueExpression, setter); } public static T SetNotifyProperty<T>(this PropertyChangedEventHandler postHandler, PropertyChangingEventHandler preHandler, T newValue, Expression<Func<T>> oldValueExpression, Action<T> setter) { Func<T> getter = oldValueExpression.Compile(); T oldValue = getter(); if (!oldValue.Equals(newValue)) { var body = oldValueExpression.Body as System.Linq.Expressions.MemberExpression; var propInfo = body.Member as PropertyInfo; string propName = body.Member.Name; // Get the target object var targetExpression = body.Expression as ConstantExpression; object target = targetExpression.Value; if (preHandler != null) preHandler(target, new PropertyChangingEventArgs(propName)); // Use Reflection to do the set: // propInfo.SetValue(target, newValue, null); //var compiledSetter = setter.Compile(); setter(newValue); if (postHandler != null) postHandler(target, new PropertyChangedEventArgs(propName)); } return newValue; } }