有人可以解释一下使用Auto-Implemented Properties c#背后的想法吗?
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有动力使用私有字段的属性,因此我们可以确定如何访问私有字段.但在这里 - 就像从一开始就将这个领域定义为公开一样.没有?
将字段定义为"public const"或将其定义为具有get-only属性之间是否存在差异?
我正在构建一个C#表达式到Javascript的转换器,沿着Linq-to-SQL的路线,但我遇到了编译器生成的表达式树的问题.
我遇到的特殊问题是处理MemberExpression编译器生成的值,但是没有CompilerGeneratedAttribute在其类型上指定的值.
这是我一直在尝试的简化版本:
void ProcessMemberExpression(MemberExpression memberX) {
var expression = memberX.Expression;
var expressionType = expression.Type;
var customAttributes = expressionType.GetCustomAttributes(true);
var expressionTypeIsCompilerGenerated = customAttributes.Any(x => x is CompilerGeneratedAttribute);
if (expressionTypeIsCompilerGenerated) {
var memberExpressionValue = Expression.Lambda(memberX).Compile().DynamicInvoke();
... do stuff ...
}
else {
... do other stuff ...
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我打开了一个Visual Studio调试会话,我发现它(在立即窗口中运行):
expressionType.Name
"<>c__DisplayClass64"
expressionType.GetCustomAttributes(true)
{object[0]}
expressionType.GetCustomAttributes(true).Length
0
Run Code Online (Sandbox Code Playgroud)
所以我在这里有一个显然是编译器生成的类,没有自定义属性,因此没有CompilerGeneratedAttribute!因此,do other stuff当我打算公正时,我的代码将会do stuff.
如果有人能帮助我,我会非常感激.如果可能的话,我真的宁愿做任何肮脏的事情就像匹配类似的expressionType.Name东西<>.*__DisplayClass.
简单问题:抽象属性是否创建了私有支持字段?例:
public abstract Name { get; set; }
Run Code Online (Sandbox Code Playgroud)
这会创建一个私人支持领域吗?我想强制任何派生此属性的类使用自己的支持字段,而不是编译器创建的支持字段.
这里是JustDecompile反编译的一个ASP.NET Core nuget包,我看不懂<>c.<>9. 我发现他们没有声明,很奇怪,Nuget包名是Microsoft.Extensions.FileProviders.Physical,类文件名是PhysicalFilesWatcher.