Method 1:
Check if code is valid and then test if it contains method calls that are not allowed but you have to ignore string literals.
However, this will not stop the user to invoke methods by name if Reflection
is available.
bool validCode = true;
string[] unallowedMethods = new string[]
{
"Array.BinarySearch",
...
};
string sourceCode = ...;
ICodeCompiler codeCompiler = ...;
CompilerResults results = codeCompiler.CompileAssemblyFromSource(parameters, sourceCode);
validCode = result.Errors.Count == 0;
if (validCode)
foreach (method in unallowedMethods)
if (sourceCode.Contains(method)))
//improve this by checking if occurrence is not a string literal in program
{
validCode = false;
break;
}
Method 2:
Test if a method calls another method using Mono Cecil:
Take a look at this answer
static class MethodDefinitionExtensions
{
public static bool CallsMethod(this MethodDefinition caller,
MethodDefinition callee)
{
return caller.Body.Instructions.Any(x =>
x.OpCode == OpCodes.Call && x.Operand == callee);
}
}
class Program
{
private static AssemblyDefinition _assembly = AssemblyDefinition.ReadAssembly(
System.Reflection.Assembly.GetExecutingAssembly().Location);
private static void Method1()
{
Method2();
}
private static void Method2()
{
Method1();
Method3();
}
private static void Method3()
{
Method1();
}
private static IEnumerable<MethodDefinition> GetMethodsCalled(
MethodDefinition caller)
{
return caller.Body.Instructions
.Where(x => x.OpCode == OpCodes.Call)
.Select(x => (MethodDefinition)x.Operand);
}
private static MethodDefinition GetMethod(string name)
{
TypeDefinition programType = _assembly.MainModule.Types
.FirstOrDefault(x => x.Name == "Program");
return programType.Methods.First(x => x.Name == name);
}
public static void Main(string[] args)
{
MethodDefinition method1 = GetMethod("Method1");
MethodDefinition method2 = GetMethod("Method2");
MethodDefinition method3 = GetMethod("Method3");
Debug.Assert(method1.CallsMethod(method3) == false);
Debug.Assert(method1.CallsMethod(method2) == true);
Debug.Assert(method3.CallsMethod(method1) == true);
Debug.Assert(GetMethodsCalled(method2).SequenceEqual(
new List<MethodDefinition> { method1, method3 }));
}
}