标签: readonly-attribute

自动修复和只读属性

让我们考虑同一个非常简单的实体的两个版本(一个带有只读属性):

public class Client
{
    public Guid Id { get; set; }

    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

public class Client
{
    public Client(Guid id, string name)
    {
        this.Id = id;
        this.Name = name;
    }

    public Guid Id { get; }

    public string Name { get; }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用Autofixture时,它们将正常工作,并且可以同时使用它们。当我尝试使用预先定义参数之一时,问题就开始了。with()方法:

var obj = this.fixture.Build<Client>().With(c => c.Name, "TEST").Build();
Run Code Online (Sandbox Code Playgroud)

这将引发错误

System.ArgumentException:属性“名称”是只读的。

但是似乎Autofixture知道如何使用构造函数!而且看来实际Build<>()方法不是创建对象的实例Create()!如果构建仅准备具有设置属性的构建器,然后创建将实例化对象,则它将与只读属性一起正常工作。

那么为什么在这里使用这种(误导)策略呢?我在这里找到了一个答案,指出它是通过测试来放大反馈,但是我看不出使用的用处,FromFactory()尤其是当参数列表很广时。在Build()方法之间移动对象实例化Create()会更直观吗?

c# readonly-attribute autofixture

11
推荐指数
2
解决办法
2562
查看次数

PropertyGrid对象级别的readonly属性

我想在我的中显示一个类的多个实例PropertyGrid.这个类看起来像这样:

public class Parameter
{
    [Description("the name")]
    public string Name { get; set; }

    [Description("the value"), ReadOnly(true)]
    public string Value { get; set; }

    [Description("the description")]
    public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有很多类的实例TreeView.当我在我选择其中一个时TreeView,属性会PropertyGrid按预期显示.到目前为止一切顺利,但我想以下列方式自定义此行为:

对于每个单个实例,我希望能够阻止用户修改特定属性.通过ReadOnly(true)在我的类中设置(如上面的示例中所示),Value将在类级别禁用所有属性.

经过一些研究后,我发现以下解决方案让我有机会在运行时启用/禁用特定属性:

PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this)["Value"];

ReadOnlyAttribute attr = 
        (ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];

FieldInfo isReadOnly = attr.GetType().GetField(
        "isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);

isReadOnly.SetValue(attr, false);
Run Code Online (Sandbox Code Playgroud)

这种方法运行得很好但不幸的是也只​​在类级别上.这意味着,如果我设定ValueisReadOnlyfalse,所有我的Parameter-objects拥有 …

.net c# propertygrid propertydescriptor readonly-attribute

8
推荐指数
1
解决办法
1万
查看次数

Android Studio:无法将新类或任何文件添加到项目中

我的Android工作得很好,能够在我的项目中添加新的xml,活动或任何新文件.直到最近,每当我添加新类或任何新文件时它都会出错.它总是说:

无法解析模板"类"错误消息:无法修改只读目录'C:\ Users\jay\Desktop\CurLoc\app\src\main\java\com\example\jay\curloc'.

我已经尝试通过取消选中"属性"中的"只读"框来更改项目所在文件夹的属性.我也一遍又一遍地重新启动计算机和Android Studio并刷新项目但没有任何效果.请帮忙.

readonly readonly-attribute android-studio

8
推荐指数
1
解决办法
1814
查看次数

可以使用指针修改只读字段吗?但为什么?

我来自 C++,发现 C++ 和 C# 中的指针之间的行为非常不同。

我惊讶地发现这段代码可以编译......甚至......工作完美。

class C
{
    private readonly int x = 0;
    unsafe public void Edit()
    {
        fixed (int* p = &x)
        {
           *p = x + 1;
        }
        Console.WriteLine($"Value: {x}");
    }
}
Run Code Online (Sandbox Code Playgroud)

这让我很困惑,即使在 C++ 中我们也有保护const对象的机制(const在 C++ 中与readonly在 C# 中几乎相同,而不是const在 C# 中),因为我们没有足够的理由通过指针随意修改 const 值。

进一步探索,我发现在 C# 中没有等效于 C++ 的低级 const 指针,它类似于:

readonly int* p
Run Code Online (Sandbox Code Playgroud)

在 C# 中,如果有的话。

那么 p 只能读取指向的对象,不能写入。

对于const对象,C#禁止尝试检索其地址。

在 C++ …

c# c++ pointers readonly-attribute language-lawyer

8
推荐指数
1
解决办法
308
查看次数

ReadOnly(true)是否与Html.EditorForModel一起使用?

请考虑以下设置:

模型:

public class Product
{
    [ReadOnly(true)]
    public int ProductID
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }
}
Run Code Online (Sandbox Code Playgroud)

视图:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage<MvcApplication4.Models.Product>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <%= Html.EditorForModel() %>
</asp:Content>
Run Code Online (Sandbox Code Playgroud)

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new Product
            {
                ProductID = 1,
                Name = "Banana"
            });
    }
}
Run Code Online (Sandbox Code Playgroud)

结果如下: 替代文字

我期待该ProductID属性不会通过该ReadOnly(true)属性进行编辑.这支持吗?如果没有,有没有办法提示ASP.NET MVC我的模型的某些属性是只读的?我不想隐藏ProductID通过[ScaffoldColumn(false)].

asp.net-mvc readonly-attribute asp.net-mvc-2

7
推荐指数
2
解决办法
1万
查看次数

如何使用 Moq 设置只读属性?

我正在尝试使用 Moq 进行单元测试。这是示例代码:

public class ConcreteClass
{
    private readonly FirstPropery firstProperty;
    private readonly SecondProperty secondProperty;

    public ConcreteClass(firstProperty, secondProperty)
    {
        this.firstProperty = firstProperty;
        this.secondProperty = secondProperty;
    }
}

[TestMethod]
    var concreteClassMock = new Mock<ConcreteClass>() { CallBase = true };
Run Code Online (Sandbox Code Playgroud)

在我的测试方法中,我想设置firstProperty引用一个真实的对象FirstProperty对象(由工厂创建),然后用它来测试另一个对象的行为。有什么方法可以实现吗?

c# moq readonly-attribute

6
推荐指数
2
解决办法
1万
查看次数

只读属性和ngOnInit

只能在构造函数中分配只读属性,但是不鼓励使用angular属性,有时甚至无法使用构造函数进行某些初始化,而是使用了角度挂钩ngOnInit。有什么办法可以将ngOnInit标记为关于readonly属性的构造函数,以便我可以对本质上不可变的属性(仅在ngOnInit中分配一次)使用readonly吗?

编辑:澄清:我不寻找替代方法来声明只读属性。我想以常规方式声明它们。我认为仅仅为了进行静态检查就不值得在可读性上进行权衡。我希望会有一些注释,例如tslint可以忽略ngOnInit内部的不变性。

从目前为止的答案中,我想将它们分配为(this as any).foo = bar;与我想做的最接近的答案,尽管我认为它比仅保留只读项还要难看。

javascript readonly-attribute typescript angular

6
推荐指数
2
解决办法
536
查看次数

如何在readonly字段上禁用jquery验证?

我正在使用的产品使用JQuery验证和MVC3在幕后工作.我有一个字段,我切换为只读(基于下拉列表的值是否> 0).

诀窍是,当该字段不是只读时需要该字段,并且当它是只读时需要提交而没有错误,无论它是否有数据.

我应该怎么做呢?

jquery jquery-validate readonly-attribute asp.net-mvc-3

5
推荐指数
1
解决办法
9045
查看次数

是否可以在iOS中使用KVC将值设置为只读属性?

我试图在这里描述我的困惑.如果这个问题需要任何修改,请告诉我.

Accordind to Apple documentaition: readonly属性的参考 -

Apple文档链接

所以我们不能将值设置为readonly属性,因为它的setter不会被创建.但是我已经创建了一个用于测试它的演示项目.我正在和你分享我的代码示例.

这是"ModelTest1.h"类.它有一个只读属性.

#import <Foundation/Foundation.h>

@interface ModelTest1 : NSObject

@property (readonly) NSMutableArray  *arrTest;

@end
Run Code Online (Sandbox Code Playgroud)

现在我正在使用KVC设置它的价值.因为这个属性是readonly,所以在运行时它应该给我一个错误,但它没有发生.

- (void)readonlyTest
{
    ModelTest1  *obj1 = [ModelTest1 new];

    NSLog(@"obj1.arrTest before = %@",obj1.arrTest);

    [obj1 setValue:[NSMutableArray new] forKey:@"arrTest"];

    NSLog(@"obj1.arrTest after = %@",obj1.arrTest);

    [obj1.arrTest addObject:@"Str 1"];

    NSLog(@"obj1.arrTest after 2 = %@",obj1.arrTest);

    [obj1 release];
}
Run Code Online (Sandbox Code Playgroud)

输出是:

控制台输出

我不知道为什么内存在NSMutableArray中被分配,即使它只是readonly.为什么字符串会添加到此只读数组中.

我还使用KVC对NSString进行了内存分配测试并得到了相同的结果.使用KVC将值设置为只读字符串.

这是我对readonly属性的setValue的困惑.

所以我误解了一些关于readonly财产的事情?或者还有其他事情发生?

objective-c readonly-attribute ios

5
推荐指数
1
解决办法
612
查看次数

为什么Point.Offset()没有在readonly结构中给出编译器错误?

也许我误解了readonly结构的概念,但我认为这段代码不应该编译:

public readonly struct TwoPoints
{
    private readonly Point one;
    private readonly Point two;

    void Foo()
    {
        // compiler error:  Error CS1648  Members of readonly field 'TwoPoints.one'
        // cannot be modified (except in a constructor or a variable initializer)
        one.X = 5;

        //no compiler error! (and one is not changed)
        one.Offset(5, 5);
    }
 }
Run Code Online (Sandbox Code Playgroud)

(我正在使用C#7.3.)我错过了什么?

.net c# struct readonly-attribute

4
推荐指数
1
解决办法
125
查看次数