// Copyright (C) Stichting Deltares 2019. All rights reserved.
//
// This file is part of Riskeer.
//
// Riskeer is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using Core.Common.Gui.Properties;
namespace Core.Common.Gui.Attributes
{
///
/// Marks property as a conditional visible property. When this attribute is declared
/// on a property, the declaring class should have a public method marked with
/// to be used to evaluate if
/// that property should be visible or not.
///
///
///
/// This attribute provides a run-time alternative to .
[AttributeUsage(AttributeTargets.Property)]
public sealed class DynamicVisibleAttribute : Attribute
{
///
/// Determines whether the property is visible or not.
///
/// The object.
/// The name of the property of .
/// true if the property is visible, false otherwise.
/// Thrown when
/// does not correspond to a public property of .
/// Thrown when there isn't a single method
/// declared on marked with
/// that is matching the signature defined by .
public static bool IsVisible(object value, string propertyName)
{
if (string.IsNullOrEmpty(propertyName))
{
return BrowsableAttribute.Default.Browsable;
}
if (!IsPropertyDynamicallyVisible(value, propertyName))
{
return BrowsableAttribute.Default.Browsable;
}
DynamicVisibleValidationMethodAttribute.IsPropertyVisible isPropertyVisibleDelegate = DynamicVisibleValidationMethodAttribute.CreateIsVisibleMethod(value);
return isPropertyVisibleDelegate(propertyName);
}
private static bool IsPropertyDynamicallyVisible(object obj, string propertyName)
{
MemberInfo propertyInfo = obj.GetType().GetProperty(propertyName);
if (propertyInfo == null)
{
throw new MissingMemberException(string.Format(CultureInfo.CurrentCulture,
Resources.Could_not_find_property_0_on_type_1_,
propertyName, obj.GetType()));
}
return IsDefined(propertyInfo, typeof(DynamicVisibleAttribute));
}
}
}