using System;
using System.Linq;
using System.Reflection;
namespace NLua
{
///
/// Allows the user to specify the name of the member when accessed in Lua
///
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field)]
public sealed class LuaMemberAttribute : Attribute
{
///
/// The name of the member when used accessed in Lua
///
public string Name { get; set; }
public static MethodInfo[] GetMethodsForType(Type type, string methodName, BindingFlags bindingFlags, Type[] signature)
{
return type.GetMethods(bindingFlags).Where(m =>
{
if (m.GetCustomAttribute() != null)
return false;
if (m.GetCustomAttribute() != null)
{
var attr = m.GetCustomAttribute();
return attr.Name == methodName && m.GetParameters().Select(p => p.ParameterType).SequenceEqual(signature);
}
return m.Name == methodName && m.GetParameters().Select(p => p.ParameterType).SequenceEqual(signature);
}).ToArray();
}
public static MethodInfo[] GetMethodsForType(Type type, string methodName, BindingFlags bindingFlags)
{
return type.GetMethods(bindingFlags).Where(m =>
{
if (m.GetCustomAttribute() != null)
return false;
if (m.GetCustomAttribute() != null)
{
var attr = m.GetCustomAttribute();
return attr.Name == methodName;
}
return m.Name == methodName;
}).ToArray();
}
public static MemberInfo[] GetMembersForType(Type type, string memberName, BindingFlags bindingFlags)
{
return type.GetMembers(bindingFlags).Where(m =>
{
if (m.GetCustomAttribute() != null)
return false;
if (m.GetCustomAttribute() != null)
{
var attr = m.GetCustomAttribute();
return attr.Name == memberName;
}
return m.Name == memberName;
}).ToArray();
}
}
}