GetWindows() with reflection
Closed this issue · 1 comments
swharden commented
Use reflection to make it easy to list available windows and get window by name. Something like
cbWindow.Items.AddRange
(
typeof(FftSharp.Window)
.GetMethods()
.Select(x => x)
.Where(x => x.GetParameters().Length == 1)
.Where(x => x.ReturnType == typeof(double[]))
.OrderBy(x => x.Name)
.Select(x => x.Name)
.ToArray()
);
MethodInfo[] infos = typeof(FftSharp.Window).GetMethods(BindingFlags.Public | BindingFlags.Static);
foreach (var info in infos)
{
if (info.Name == cbWindow.Text)
{
double[] window = (double[])info.Invoke(null, new object[] { spec.FftSize });
spec.SetWindow(window);
break;
}
}
swharden commented
d'oh! This already exists. FftSharp.Window.GetWindowByName(windowName, pointCount)
public static string[] GetWindowNames()
{
return typeof(Window)
.GetMethods(BindingFlags.Public | BindingFlags.Static)
.Where(x => x.ReturnType.Equals(typeof(double[])))
.Where(x => x.GetParameters().Length == 1)
.Where(x => x.GetParameters()[0].ParameterType == typeof(int))
.Select(x => x.Name)
.ToArray();
}
public static double[] GetWindowByName(string windowName, int pointCount)
{
MethodInfo[] windowInfos = typeof(Window)
.GetMethods(BindingFlags.Public | BindingFlags.Static)
.Where(x => x.ReturnType.Equals(typeof(double[])))
.Where(x => x.GetParameters().Length == 1)
.Where(x => x.GetParameters()[0].ParameterType == typeof(int))
.Where(x => x.Name == windowName)
.ToArray();
if (windowInfos.Length == 0)
throw new ArgumentException($"invalid window name: {windowName}");
object[] parameters = new object[] { pointCount };
double[] result = (double[])windowInfos[0].Invoke(null, parameters);
return result;
}