如何在 C# 中运行时锁定/解锁字段?

发布于 2024-08-18 04:41:30 字数 222 浏览 6 评论 0原文

我可以在运行时锁定/解锁字段或对象以防止写入吗?换句话说,比如在运行时将对象暂时更改为只读...

例如:

int x = 5; // x is 5  
LockObject(x);  
x = 7; // no change  
UnlockObject(x);  
x = 10; // x is 10

如果没有,您能给我一些可能的解决方案吗?

Can I lock/unlock fields or objects at runtime against writing? In other words something like changing objects to read-only temporarily at runtime...

For example:

int x = 5; // x is 5  
LockObject(x);  
x = 7; // no change  
UnlockObject(x);  
x = 10; // x is 10

if not can you give me some possible solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

英雄似剑 2024-08-25 04:41:30

您可以使用访问器来执行此操作...

public class X
{
    private bool locked = false;

    private int lockedVar;
    public int LockedVar
    {
        get { return lockedVar; }
        set { if (!locked) lockedVar = value; }
    }

    public void LockObject() { locked = true; }
    public void UnlockObject() { locked = false; }
}

You can use accessors to do this...

public class X
{
    private bool locked = false;

    private int lockedVar;
    public int LockedVar
    {
        get { return lockedVar; }
        set { if (!locked) lockedVar = value; }
    }

    public void LockObject() { locked = true; }
    public void UnlockObject() { locked = false; }
}
绅刃 2024-08-25 04:41:30

将其包装在属性中,然后使用布尔标志作为“锁”。

您还可以使用结构来处理此类逻辑。

public struct LockableInt
{
    private int _value;
    public bool Locked;

    public void Lock(bool locked) {Locked = locked;}

    public int Value
    {
        get 
        { return _value; }
        set 
        { if (!Locked) _value = value; }
    }

    public override string ToString()
    {
        return _value.ToString();
    }
}

示例客户端代码:

public static void RunSnippet()
{
    LockableInt li = new LockableInt();
    li.Value = 5;
    Console.WriteLine(li.ToString());
    li.Lock(true);
    li.Value = 6;
    Console.WriteLine(li.ToString());
    li.Lock(false);
    li.Value = 7;
    Console.WriteLine(li.ToString());

}

输出:

5
5
7
按任意键继续...

Wrap it in a property and then use a boolean flag as the "lock".

You can also use a struct to handle this type of logic.

public struct LockableInt
{
    private int _value;
    public bool Locked;

    public void Lock(bool locked) {Locked = locked;}

    public int Value
    {
        get 
        { return _value; }
        set 
        { if (!Locked) _value = value; }
    }

    public override string ToString()
    {
        return _value.ToString();
    }
}

Sample client code:

public static void RunSnippet()
{
    LockableInt li = new LockableInt();
    li.Value = 5;
    Console.WriteLine(li.ToString());
    li.Lock(true);
    li.Value = 6;
    Console.WriteLine(li.ToString());
    li.Lock(false);
    li.Value = 7;
    Console.WriteLine(li.ToString());

}

Output:

5
5
7
Press any key to continue...

雨巷深深 2024-08-25 04:41:30

感谢大家的帮助。这是一个更通用的替代方案。
我决定使用冻结/解冻会更方便,因为锁定/解锁存在于多线程编程的上下文中。

public class FreezeableValue<T>  
{  
 private bool frozen;  
 private T value;

 public FreezeableValue()
 {
    frozen = false;
 }

 public FreezeableValue(T value)
 {
    this.value = value;
    frozen = false;
 }

 public void Freeze(bool frozen)
 {
    this.frozen = frozen;
 }

 public T Value
 {
    get { return value; }
    set
    {
        if (!frozen) this.value = value;
    }
 }
}

Thanks all for help. Here is a more generalized alternative.
I have decided that it would be more convenient to use freeze/unfreeze since lock/unlock exists in the context of multithreading programming.

public class FreezeableValue<T>  
{  
 private bool frozen;  
 private T value;

 public FreezeableValue()
 {
    frozen = false;
 }

 public FreezeableValue(T value)
 {
    this.value = value;
    frozen = false;
 }

 public void Freeze(bool frozen)
 {
    this.frozen = frozen;
 }

 public T Value
 {
    get { return value; }
    set
    {
        if (!frozen) this.value = value;
    }
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文