[询问]如何从特定类设置和获取属性值 [英] [Ask] How to set and get property value from a specific class

查看:90
本文介绍了[询问]如何从特定类设置和获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试创建属性时遇到问题.这是示例

Hi, I have problem when trying to create property. Here''s the example

public class Debt //Tanpa constructor
{
    [Description("This is readonly property"), ReadOnly(true)]
    public bool IsAccountLocked
    {
        get;
        set;
    }
}

public class Credit
{
    //Only get from this class and can not set value of IsAccountLocked
    Debt debt = new Debt();
    public string GetAccountName
    {
        get
        {
            if (debt.IsAccountLocked)
                return string.Empty;
            else
                return "CFX001";
        }
    }
}

public class UserAccounts
{
    //Only set from this class and can not get value os IsAccountLocked
    Debt debt = new Debt();
    internal void SetLock(string Account)
    {
        debt.IsAccountLocked = true;
    }
}



我有3节课.债务,信用和UserAcconts.在Debt中有一个名为IsAccountLocked的布尔属性.如何使该属性只能通过Credit获得"GET",而只能通过UserAccounts获得"SET"?换句话说,如果Credit尝试设置该属性,它将生成错误.我还可以在Credit中隐藏"SET"吗?在此先谢谢您.



I have 3 classes. Debt, Credit and UserAcconts. In Debt there''s a boolean property called IsAccountLocked. How to make this property can only be "GET" from Credit and only be "SET" from UserAccounts? In other words, if Credit tries to SET the property it generates error. Can I also hide "SET" from Credit. Thanks in advance.

推荐答案

您不能.
您唯一可以做的就是将访问修饰符更改为internal或protected,但是您不能根据调用类的类型来设置修饰符. IE.您不能在一个类中拥有私有或内部的getter,而在另一类中拥有公共的getter-C#中没有为此提供机制的

您可能可以在运行时通过反射和调用堆栈来执行此操作,但这是执行操作的长毛方式.

我自己可能会创建一个带有受保护的getter和setter的抽象基类,然后从中派生两个类-一个带有公共getter的类,一个带有公共setter的类.

不过,就维护而言,这是一个令人讨厌的想法-它需要一个很好的注释标准.
You can''t.
The only thing you can do is change the access modifiers to internal or protected, but you can''t set an modifier depending on the type of the calling class. I.e. You can''t have a getter that is private or internal in one class, and public in another - there is no mechanism for that in C#

You could probably do it at run time via reflection and the call stack, but that is a hairy way to do things.

Myself, I would probably create an abstract base class with protected getter and setter then derive two classes from that - one with a public getter, and one with a public setter.

It''s a nasty idea though, as far as maintenance goes - it would need a good standard of commenting.


我认为c#中没有语言结构可以做到这一点.我看到两种方法:
1)将调用方传递给方法,然后检查方法中的类型.
A)分离方法
I don''t think there is a language construct in c# to do this. I see two approaches:
1) Pass caller to the methods, and check type in the method.
A) Separating methods
public class Debt //Tanpa constructor
    {
        private bool _IsAccountLocked;
        public void setIsAccountLocked(object caller, bool value)
        {
           if(!caller is UserAccounts)
           {
              throw new Exception("Only a UserAccount instance can set this");
           }
           _IsAccountLocked = value;
        }

        public bool IsAccountLocked
        {
           get { return _IsAccountLocked; }
        }
    }



B)索引属性(khmmm ...)



B) Indexed property (khmmm...)

public class Debt //Tanpa constructor
    {
        private bool _IsAccountLocked;
        public bool IsAccountLocked[object caller]
        {
           get { return _IsAccountLocked; }
           set {
             if(!caller is UserAccounts)
             {
                throw new Exception("Only a UserAccount instance can set this");
             }
             _IsAccountLocked = value;
           }
        }
    }


在这两种情况下,调用方都应为 caller 参数传递this.
2)如果您不信任调用方,则可以从堆栈中获取调用类:
http://www.techdreams.org/microsoft/c-how-to-get-calling-class-name-method-name-using-stackframe-class/5815-20110507 [


In both cases, the caller should pass this for the caller parameter.
2) If you can''t trust the caller, you can get the calling class from the stack:
http://www.techdreams.org/microsoft/c-how-to-get-calling-class-name-method-name-using-stackframe-class/5815-20110507[^]


尽管解决方案1和解决方案2告诉您这是不可能的,有一种解决方法,但是您需要将这些类放在两个不同的程序集中. br/>
组装#1:
Despite Solution 1 and Solution 2 tells you that this is not possible, there is a way to do it, but then you need to put the classes in two different assemblies.

Assembly #1:
public class Debt {
    //...
    internal bool IsAccountLocked { get; set; } // internal allows access by the type of the same assembly...
    //...
}

public class UserAccounts { /* ... */ } //...so, Debt.IsAccountLocked is accessible here



组装2:



Assembly #2:

public class Credit { /* ... */ } //Debt.IsAccountLocked is not accessible, because this is a different assembly





如果尝试在单个程序集中进行,则在属性访问上完全不可能达到相同的效果.

—SA





It is strictly impossible to achieve the same effect on the property access if you try to do it in a single assembly.

—SA


这篇关于[询问]如何从特定类设置和获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆