草庐IT

Getters-Setter

全部标签

C# 快捷方式或速记 getter setter

有没有在C#中创建getter和setter的捷径?publicstringfname{get;set;}生成{get;有简写吗?设置;}? 最佳答案 是的,输入prop并按TAB键。VisualStudio有一个自动属性片段。对于具有公共(public)获取和私有(private)设置的属性,您可以使用propg并按TAB。对于完整的非自动属性,您可以使用propfull并按TAB键。 关于C#快捷方式或速记gettersetter,我们在StackOverflow上找到一个类似的问题

c# - 创建属性 setter 委托(delegate)

我已经创建了将属性lambda转换为委托(delegate)的方法:publicstaticDelegateMakeGetter(Expression>propertyLambda){varresult=Expression.Lambda(propertyLambda.Body).Compile();returnresult;}publicstaticDelegateMakeSetter(Expression>propertyLambda){varresult=Expression.Lambda(propertyLambda.Body).Compile();returnresult;}

c# - 创建属性 setter 委托(delegate)

我已经创建了将属性lambda转换为委托(delegate)的方法:publicstaticDelegateMakeGetter(Expression>propertyLambda){varresult=Expression.Lambda(propertyLambda.Body).Compile();returnresult;}publicstaticDelegateMakeSetter(Expression>propertyLambda){varresult=Expression.Lambda(propertyLambda.Body).Compile();returnresult;}

c# - Moq - 如何验证属性值是否通过 setter 设置

考虑这个类:publicclassContent{publicvirtualboolIsCheckedOut{get;privateset;}publicvirtualvoidCheckOut(){IsCheckedOut=true;}publicvirtualvoidCheckIn(){//DoNothingfornowasdemonstratingfalsepositivetest.}}Checkin方法是有意为空的。现在我有几个测试方法来验证调用每个方法的状态。[TestMethod]publicvoidCheckOutSetsCheckedOutStatusToTrue(){C

c# - Moq - 如何验证属性值是否通过 setter 设置

考虑这个类:publicclassContent{publicvirtualboolIsCheckedOut{get;privateset;}publicvirtualvoidCheckOut(){IsCheckedOut=true;}publicvirtualvoidCheckIn(){//DoNothingfornowasdemonstratingfalsepositivetest.}}Checkin方法是有意为空的。现在我有几个测试方法来验证调用每个方法的状态。[TestMethod]publicvoidCheckOutSetsCheckedOutStatusToTrue(){C

c# - 使用反射,我如何检测具有 setter 的属性?

我有这段代码来遍历一个对象并通过反射获取它的所有属性:foreach(varpropertyInfointypeof(TBase).GetProperties(BindingFlags.Public|BindingFlags.Instance)){varoldValue=propertyInfo.GetValue(oldVersion,null);}我如何进行检查以仅查看具有“Set”的属性?(我想忽略只读值-只是“获取”。) 最佳答案 PropertyInfo.CanWrite(documentation)或PropertyInf

c# - 使用反射,我如何检测具有 setter 的属性?

我有这段代码来遍历一个对象并通过反射获取它的所有属性:foreach(varpropertyInfointypeof(TBase).GetProperties(BindingFlags.Public|BindingFlags.Instance)){varoldValue=propertyInfo.GetValue(oldVersion,null);}我如何进行检查以仅查看具有“Set”的属性?(我想忽略只读值-只是“获取”。) 最佳答案 PropertyInfo.CanWrite(documentation)或PropertyInf

c# - 为什么在显式 getter-only 接口(interface)实现上使用私有(private) setter 是非法的?

我倾向于支持显式接口(interface)实现而不是隐式接口(interface)实现,因为我认为针对接口(interface)而不是针对实现进行编程通常更可取,而且在处理Web服务时,这通常是必需的。也就是说,我想知道为什么以下对于显式接口(interface)声明是非法的,而对于隐式接口(interface)声明是合法的:interfaceIConnection{stringConnectionString{get;}}classConnection1:IConnection{//privatesetisillegal,won'tcompilestringIConnection.C

c# - 为什么在显式 getter-only 接口(interface)实现上使用私有(private) setter 是非法的?

我倾向于支持显式接口(interface)实现而不是隐式接口(interface)实现,因为我认为针对接口(interface)而不是针对实现进行编程通常更可取,而且在处理Web服务时,这通常是必需的。也就是说,我想知道为什么以下对于显式接口(interface)声明是非法的,而对于隐式接口(interface)声明是合法的:interfaceIConnection{stringConnectionString{get;}}classConnection1:IConnection{//privatesetisillegal,won'tcompilestringIConnection.C

c# - 自动实现的 getter 和 setter 与公共(public)字段

我看到很多C#类的示例代码都是这样做的:publicclassPoint{publicintx{get;set;}publicinty{get;set;}}或者,在旧代码中,具有显式私有(private)支持值但没有新的自动实现的属性:publicclassPoint{privateint_x;privateint_y;publicintx{get{return_x;}set{_x=value;}}publicinty{get{return_y;}set{_y=value;}}}我的问题是为什么。执行上述操作与仅将这些成员设置为公共(public)字段(如下所示)之间在功能上有什么区别