1+ using System . Collections . Generic ;
2+ using System . Reflection ;
3+ using Autodesk . Revit . DB ;
4+ using ExpressionTreeToString ;
5+
6+ // (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md
7+
8+ namespace System . Linq . Expressions
9+ {
10+ internal static class MethodCallExpressionExtensions
11+ {
12+ public static string ToCeSharp ( this MethodCallExpression methodCallExpression )
13+ {
14+ string syntax = null ;
15+ if ( methodCallExpression . Object is ParameterExpression )
16+ {
17+ var uniformMethodCallExpression = methodCallExpression . Update ( Expression . Parameter ( methodCallExpression . Object . Type , "item" ) , methodCallExpression . Arguments ) ;
18+ syntax = uniformMethodCallExpression . ToString ( "C#" ) ;
19+ }
20+ if ( methodCallExpression . Object == null )
21+ {
22+ var arguments = new List < Expression > ( ) ;
23+ foreach ( var arg in methodCallExpression . Arguments )
24+ {
25+ if ( arg is ParameterExpression )
26+ {
27+ var isDocument = arg . Type == typeof ( Document ) ;
28+ arguments . Add ( Expression . Parameter ( arg . Type , isDocument ? "document" : "item" ) ) ;
29+ continue ;
30+
31+ }
32+ if ( arg is MemberExpression member )
33+ {
34+ var isDocument = member . Expression . Type == typeof ( Document ) ;
35+ arguments . Add ( Expression . Property ( Expression . Parameter ( member . Expression . Type , isDocument ? "document" : "item" ) , member . Member as PropertyInfo ) ) ;
36+ continue ;
37+ }
38+ arguments . Add ( arg ) ;
39+ }
40+
41+
42+
43+ var uniformMethodCallExpression = methodCallExpression . Update ( null , arguments ) ;
44+ //var syntaxb = methodCallExpression.ToString("C#");
45+ syntax = uniformMethodCallExpression . ToString ( "C#" ) ;
46+ }
47+ return syntax ;
48+ }
49+ }
50+ }
0 commit comments