Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make FunctionRegistry enumerable. #20

Closed
wants to merge 2 commits into from

Conversation

mibe
Copy link
Contributor

@mibe mibe commented May 13, 2016

Mostly copy&paste from ConstantRegistry.

mibe added 2 commits May 13, 2016 03:32
Mostly copy&paste from ConstantRegistry.
"'Delegate' does not contain a definition for 'GetMethodInfo'"
@pieterderycke
Copy link
Owner

Before I accept the pull request: could you explain me why you need this modification?

@mibe
Copy link
Contributor Author

mibe commented Jul 11, 2018

Sure. I should have added some more info before. Sorry about that.

I had the requirement to display the available functions and constants to the user (see here). I had to implement a hack using reflection to gain access to the registries. ConstantRegistry inherits IEnumerable<> already (so this was straight forward), but FunctionRegistry does not, so I thought, why not implement it for FunctionRegistry also.

But what still is missing for this requirement is actually access to both registries in CalculationEngine. Maybe make the properties FunctionRegisty & ConstantRegistry public instead of internal?

Here's the hack I'm currently using with version 0.8.7:

public static IEnumerable<ConstantInfo> GetConstants(CalculationEngine engine)
{
	return (ConstantRegistry)getProperty(engine, "ConstantRegistry");
}
		
public static IEnumerable<FunctionInfo> GetFunctons(CalculationEngine engine)
{
	FunctionRegistry registry = (FunctionRegistry)getProperty(engine, "FunctionRegistry");
	Dictionary<string, FunctionInfo> functions = (Dictionary<string, FunctionInfo>)getField(registry, "functions");
	return functions.Select(x => x.Value);
}

private static object getProperty(object @object, string propertyName)
{
	PropertyInfo property = @object.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty);
	return property.GetValue(@object);
}

private static object getField(object @object, string fieldName)
{
	FieldInfo field = @object.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField);
	return field.GetValue(@object);
}

@mibe
Copy link
Contributor Author

mibe commented Jul 11, 2018

On the other hand, instead of making the registries public, a single property returning the functions / constants would be enough (at least for me 😉):

public IEnumerable<string> AvailableFunctions
{
    get { return FunctionRegistry.Select(x => x.FunctionName); }
}

@pieterderycke
Copy link
Owner

Ok, you have a valid requirement 🙂 You can expect the feature in the comming days in the dev branch. I will nog expose an IEnumerale but IEnumerable so that you can also know the number of parameters of a function.

Btw: Jace 0.9 will also support functions with a dynamic number of arguments Max(1,2,...,n) instead of Max(1,2)
Btw: .Net Core will be supported 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants