Skip to content

Instantly share code, notes, and snippets.

@Criteo-dotnet-blog
Created November 13, 2018 10:21
Show Gist options
  • Save Criteo-dotnet-blog/2bd11a8181a8ed9ed5b04b15930a73be to your computer and use it in GitHub Desktop.
Save Criteo-dotnet-blog/2bd11a8181a8ed9ed5b04b15930a73be to your computer and use it in GitHub Desktop.
private static Func<Process, string> GetProcessNameAccessor()
{
var param = Expression.Parameter(typeof(Process), "arg");
var processInfoMember = Expression.Field(param, "processInfo");
var processNameMember = Expression.Field(processInfoMember, "processName");
var lambda = Expression.Lambda(typeof(Func<Process, string>), processNameMember, param);
return (Func<Process, string>)lambda.Compile();
}
private static readonly Func<Process, string> _getProcessNameFunc = GetProcessNameAccessor ();
public string GetProcessNameByExpression()
{
return _getProcessNameFunc(_process);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment