Skip to content

Commit 1843520

Browse files
authored
Update readme for Methods accessible (#690)
* Add information to readme for breaking change in 1.3.0 * readme
1 parent 76ac418 commit 1843520

2 files changed

Lines changed: 82 additions & 3 deletions

File tree

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,29 @@ int c = 10;
3030
db.Customers.WhereInterpolated($"City == {cityName} and Orders.Count >= {c}");
3131
```
3232

33+
## :exclamation: Breaking changes
34+
35+
### v1.3.0
36+
A breaking change is introduced in version 1.3.0 which is related to calling methods on classes.
37+
Due to security reasons, it's now only allowed to call methods on the standard predefined classes like (`bool`, `int`, `string` ...).
38+
If you want to call a method on an own custom class, annotate that class with the [DynamicLinqType](https://dynamic-linq.net/advanced-extending#dynamiclinqtype-attribute).
39+
Example:
40+
``` c#
41+
[DynamicLinqType]
42+
public class MyCustomClass
43+
{
44+
public int GetAge(int x) => x;
45+
}
46+
```
47+
48+
If it's not possible to add that attribute, you need to implement a custom [CustomTypeProvider](https://dynamic-linq.net/advanced-configuration#customtypeprovider) and set this to the `ParsingConfig` and provide that config to the dynamic call.
49+
3350
## Useful links
3451

35-
- [Website](https://dynamic-linq.net/)
52+
- [Website](https://dynamic-linq.net)
3653
- [Documentation](https://dynamic-linq.net/overview)
3754
- [Online examples](https://dynamic-linq.net/online-examples)
38-
- [nuget](https://www.nuget.org/packages/System.Linq.Dynamic.Core/)
55+
- [NuGet](https://www.nuget.org/packages/System.Linq.Dynamic.Core)
3956

4057
## Info
4158
| | |

test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,23 @@ public class CustomClassWithStaticMethod
7777
public static int GetAge(int x) => x;
7878
}
7979

80+
public class CustomClassWithMethod
81+
{
82+
public int GetAge(int x) => x;
83+
}
84+
85+
[DynamicLinqType]
86+
public class CustomClassWithMethodWithDynamicLinqTypeAttribute
87+
{
88+
public int GetAge(int x) => x;
89+
}
90+
91+
[DynamicLinqType]
92+
public class CustomClassWithStaticMethodWithDynamicLinqTypeAttribute
93+
{
94+
public static int GetAge(int x) => x;
95+
}
96+
8097
public class CustomTextClass
8198
{
8299
public CustomTextClass(string origin)
@@ -1010,7 +1027,7 @@ public void DynamicExpressionParser_ParseLambda_TupleToStringMethodCall_ReturnsS
10101027
}
10111028

10121029
[Fact]
1013-
public void DynamicExpressionParser_ParseLambda_CustomMethod()
1030+
public void DynamicExpressionParser_ParseLambda_CustomStaticMethod_WhenClassIsReturnedByCustomTypeProvider_ShouldWorkCorrect()
10141031
{
10151032
// Assign
10161033
var config = new ParsingConfig
@@ -1030,6 +1047,51 @@ public void DynamicExpressionParser_ParseLambda_CustomMethod()
10301047
Check.That(result).IsEqualTo(10);
10311048
}
10321049

1050+
[Fact]
1051+
public void DynamicExpressionParser_ParseLambda_CustomStaticMethod_WhenClassHasDynamicLinqTypeAttribute_ShouldWorkCorrect()
1052+
{
1053+
// Assign
1054+
var context = new CustomClassWithStaticMethodWithDynamicLinqTypeAttribute();
1055+
var expression = $"{nameof(CustomClassWithStaticMethodWithDynamicLinqTypeAttribute)}.{nameof(CustomClassWithStaticMethodWithDynamicLinqTypeAttribute.GetAge)}(10)";
1056+
1057+
// Act
1058+
var lambdaExpression = DynamicExpressionParser.ParseLambda(typeof(CustomClassWithStaticMethodWithDynamicLinqTypeAttribute), null, expression);
1059+
var del = lambdaExpression.Compile();
1060+
var result = (int)del.DynamicInvoke(context);
1061+
1062+
// Assert
1063+
Check.That(result).IsEqualTo(10);
1064+
}
1065+
1066+
[Fact]
1067+
public void DynamicExpressionParser_ParseLambda_CustomMethod_WhenClassHasDynamicLinqTypeAttribute_ShouldWorkCorrect()
1068+
{
1069+
// Assign
1070+
var context = new CustomClassWithMethodWithDynamicLinqTypeAttribute();
1071+
var expression = $"{nameof(CustomClassWithMethodWithDynamicLinqTypeAttribute.GetAge)}(10)";
1072+
1073+
// Act
1074+
var lambdaExpression = DynamicExpressionParser.ParseLambda(typeof(CustomClassWithMethodWithDynamicLinqTypeAttribute), null, expression);
1075+
var del = lambdaExpression.Compile();
1076+
var result = (int)del.DynamicInvoke(context);
1077+
1078+
// Assert
1079+
Check.That(result).IsEqualTo(10);
1080+
}
1081+
1082+
[Fact]
1083+
public void DynamicExpressionParser_ParseLambda_CustomMethod_WhenClassDoesNotHaveDynamicLinqTypeAttribute_ShouldThrowException()
1084+
{
1085+
// Assign
1086+
var expression = $"{nameof(CustomClassWithMethod.GetAge)}(10)";
1087+
1088+
// Act
1089+
Action action = () => DynamicExpressionParser.ParseLambda(typeof(CustomClassWithMethod), null, expression);
1090+
1091+
// Assert
1092+
action.Should().Throw<ParseException>().WithMessage("Methods on type 'CustomClassWithMethod' are not accessible");
1093+
}
1094+
10331095
// [Fact]
10341096
public void DynamicExpressionParser_ParseLambda_With_InnerStringLiteral()
10351097
{

0 commit comments

Comments
 (0)