Пример #1
0
 public static void ThrowIfFrozen(IFreezable freezable)
 {
     if (freezable.IsFrozen)
     {
         throw new InvalidOperationException("Cannot mutate frozen " + freezable.GetType().Name);
     }
 }
Пример #2
0
        public static void TestFreeze(IFreezable sut)
        {
            foreach (var property in sut.GetType().GetRuntimeProperties().Where(x => x.Name != "IsFrozen"))
            {
                var value =
                    property.PropertyType.IsValueType ?
                    Activator.CreateInstance(property.PropertyType) :
                    null;

                property.SetValue(sut, value);

                var result = property.GetValue(sut);

                Assert.Equal(value, result);
            }

            sut.Freeze();

            foreach (var property in sut.GetType().GetRuntimeProperties().Where(x => x.Name != "IsFrozen"))
            {
                var value =
                    property.PropertyType.IsValueType ?
                    Activator.CreateInstance(property.PropertyType) :
                    null;

                Assert.Throws <InvalidOperationException>(() =>
                {
                    try
                    {
                        property.SetValue(sut, value);
                    }
                    catch (Exception ex)
                    {
                        throw ex.InnerException;
                    }
                });
            }
        }
Пример #3
0
 public static bool SetRefPropertyOnce <TProperty>(this IFreezable that, ref TProperty thisProperty, TProperty value, [CallerMemberName] string callerMemberName = null)
     where TProperty : class
 {
     if (ReferenceEquals(thisProperty, value))
     {
         return(false);
     }
     if ((object)thisProperty != null)
     {
         throw new System.ArgumentException($"{that.GetType().Name}.{callerMemberName} is already set.");
     }
     thisProperty = value;
     return(true);
 }
Пример #4
0
 public static void ThrowIfNotFrozen(this IFreezable that, string name = null)
 {
     if ((object)that != null)
     {
         if (!(that.IsFrozen()))
         {
             if (name is null)
             {
                 throw new InvalidOperationException($"{that.GetType().FullName} is NOT frozen.");
             }
             else
             {
                 throw new InvalidOperationException($"{name} is NOT frozen.");
             }
         }
     }
 }
Пример #5
0
 public static void SetOrThrowIfFrozen <T>(this IFreezable that, ref T target, T value, string name = null)
 {
     if ((object)that != null)
     {
         if (that.IsFrozen())
         {
             if (name is null)
             {
                 throw new InvalidOperationException($"{that.GetType().FullName} is frozen.");
             }
             else
             {
                 throw new InvalidOperationException($"{name} is frozen.");
             }
         }
     }
     target = value;
 }