private string GetCurrentUserId() { // Grab the current user's username var changedByUserName = this.GetCurrentUserName(); // Set the "Id" variable to the name of the primary key "Id" column as defined in the config var Id = loggingConfig_User.Id; // Get the user type defined in the config Type userType = Type.GetType(loggingConfig_User.UserTypeFullName + ", " + loggingConfig_User.UserTypeAssemblyName); // Create a generic expression in order to search for the user, matching the "UserName" column defined in the config const string methodSignature = "System.Linq.Expressions.Expression`1[TDelegate] Lambda[TDelegate]" + "(System.Linq.Expressions.Expression, System.Linq.Expressions.ParameterExpression[])"; MethodInfo expression_method_Lambda = typeof(Expression).GetMethods().Single(m => m.ToString() == methodSignature); var func_type = typeof(Func <,>).MakeGenericType(userType, typeof(bool)); MethodInfo expression_generic_Lambda = expression_method_Lambda.MakeGenericMethod(func_type); var param = Expression.Parameter(userType, "p"); var exp = expression_generic_Lambda.Invoke(null, new object[] { Expression.Equal( Expression.Property(param, loggingConfig_User.UserName), Expression.Constant(changedByUserName) ), new [] { param } }); // Make a generic call to repo.Single using the recently acquired user type and expression MethodInfo repo_method_Single = repo.GetType().GetMethod("Single"); MethodInfo repo_generic_Single = repo_method_Single.MakeGenericMethod(userType); var user = repo_generic_Single.Invoke(repo, new object[] { exp }); if (user == null) { return("User: "******"\"" + changedByUserName + "\"" + " not found."); } // Get the primary key of the user return it var userIdProp = userType.GetProperty(Id); return(userIdProp.GetValue(user, null).ToString()); }