//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Reflection;

#if !WASM
namespace CommonHelpers {
#else
namespace CommonHelpers.WASM {
#endif

//**************************************************************************************************
/// <summary>
/// </summary>
public static partial class HPropertyInfo {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static string ExtractNameByGetMethod( MethodBase getMethod )
{
   string methodName = HArgChecking.NN( getMethod ).Name;
   return methodName.StartsWith( "get_" ) && methodName.Length > 4 ?
      methodName.Substring( 4 ) : throw new ArgumentException(
         "Method is not a \"get\"-method of a property." );
}

public static string ExtractNameBySetMethod( MethodBase setMethod )
{
   string methodName = HArgChecking.NN( setMethod ).Name;
   return methodName.StartsWith( "set_" ) && methodName.Length > 4 ?
      methodName.Substring( 4 ) : throw new ArgumentException(
         "Method is not a \"set\"-method of a property." );
}

//==================================================================================================

public static object GetStaticValue(Type type,string propertyName)
{
   return GetStaticValueUsingVisibilityFlags(type,propertyName,BindingFlags.Public);
}

public static void SetStaticValue(Type type,string propertyName,object value)
{
   SetStaticValueUsingVisibilityFlags(type,propertyName,BindingFlags.Public,value);
}

//--------------------------------------------------------------------------------------------------

public static object GetInstanceValue(object obj,string propertyName)
{
   return GetInstanceValueUsingVisibilityFlags(obj,propertyName,BindingFlags.Public);
}

public static void SetInstanceValue(object obj,string propertyName,object value)
{
   SetInstanceValueUsingVisibilityFlags(obj,propertyName,BindingFlags.Public,value);
}

//==================================================================================================

public static object GetStaticValueUsingVisibilityFlags( Type type,
                                                         string propertyName,
                                                         BindingFlags visibilityFlags )
{
   return type.InvokeMember( propertyName,
      visibilityFlags | BindingFlags.Static | BindingFlags.GetProperty,
      null, null, new object[0] );
}

public static void SetStaticValueUsingVisibilityFlags( Type type,
                                                       string propertyName,
                                                       BindingFlags visibilityFlags,
                                                       object value )
{
   type.InvokeMember( propertyName,
      visibilityFlags | BindingFlags.Static | BindingFlags.SetProperty,
      null, null, new object[]{value} );
}

//--------------------------------------------------------------------------------------------------

public static object GetInstanceValueUsingVisibilityFlags( object obj,
                                                           string propertyName,
                                                           BindingFlags visibilityFlags )
{
   return obj.GetType().InvokeMember( propertyName,
      visibilityFlags | BindingFlags.Instance | BindingFlags.GetProperty,
      null, obj, new object[0] );
}

public static void SetInstanceValueUsingVisibilityFlags( object obj,
                                                         string propertyName,
                                                         BindingFlags visibilityFlags,
                                                         object value )
{
   obj.GetType().InvokeMember( propertyName,
      visibilityFlags | BindingFlags.Instance | BindingFlags.SetProperty,
      null, obj, new object[]{value} );
}

//==================================================================================================

static HPropertyInfo()
{
   CommonHelpers_Core.Access();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HPropertyInfo
//**************************************************************************************************

} // CommonHelpers (.WASM)
