简体   繁体   English

我可以仅使用C#中的对象名称来访问对象属性吗?

[英]Can I access an object property just with the object name in C#?

I have a simple class with some private properties and 2 public properties (value and type). 我有一个简单的类,其中包含一些私有属性和2个公共属性(值和类型)。 I'm trying to find a way to call the object so that it returns its value property without having to call obj.value. 我试图找到一种方法来调用对象,以便它无需调用obj.value即可返回其value属性。 I don't think it's possible. 我认为这是不可能的。

Here's what I have in mind: 这就是我的想法:

public class myclass {
   private int property1;
   private int property2;
   public string value;
   public string type;

   public myclass(int property1, int property2, string value, string type)
   {
       this.property1 = property1;
       this.property2 = property2;
       this.value = value;
       this.type = type;
   }
}

var obj = new myclass(1, 2, "abc", "string");
console.write(obj.value);   // returns abc
console.write(obj.type);   // returns string
console.write(obj);    // expecting abc

Is there a way the last line returns abc with this syntax? 有没有最后一行使用这种语法返回abc的方法?

You're asking why I want to achieve something like this? 您在问我为什么要实现这样的目标? I am building a service with a Javascript programming module. 我正在使用Javascript编程模块构建服务。 The objects are variables and I want the users to access them by simply writing obj instead of obj.value . 这些对象是变量,我希望用户通过简单地编写obj而不是obj.value来访问它们。 I need this behavior in C# (and not Javascript) because the Javascript is calling back the backend objects. 我需要在C#(而不是Javascript)中使用此行为,因为Javascript正在回调后端对象。 If I would not have to keep the private properties that would be easy but I need to have them. 如果我不必保留私有财产,那将很容易,但是我需要拥有它们。

UPDATE: In order to be more clear: this class is to be called by a Javascript interpreter (Jint in this case) and the class has to update the database if its value is updated, so the need for the private properties. 更新:为了更加清晰:此类将由Javascript解释器(在本例中为Jint)调用,并且如果值被更新,则该类必须更新数据库,因此需要私有属性。 Here is my actual code: 这是我的实际代码:

public class SmartNumeric : ISmartVariable
{
    private Boolean _simulation;
    private Guid UserId;
    private long userKeyId;
    private long variableKeyId;
    private string Name;
    public string type
    {
        get {
            return "numeric";
        }
    }
    private float _value;
    public float value
    {
        get {
            return _value;
        }

        set {
            _value = value;

            if (!_simulation)
                new SmartCommon().SetValue(userKeyId, variableKeyId, value.ToString());
        }
    }

    public SmartNumeric(Guid UserId, long userKeyId, long variableKeyId, string Name, float value, Boolean simulation)
    {
        this.Name = Name;
        this._value = value;
        this.UserId = UserId;
        this.userKeyId = userKeyId;
        this.variableKeyId = variableKeyId;
        this._simulation = simulation;
    }
}

So for example a user will write its own script like 因此,例如,用户将编写自己的脚本,例如

a = 45;

which is more natural in Javascript than 这在Javascript中比

a.value = 45;

where a refers to a SmartNumeric object in C#. 其中,a表示C#中的SmartNumeric对象。 The object receives an update and this is where I need to go back to the database to update the value and need the additional properties. 该对象接收更新,这是我需要返回数据库以更新值并需要其他属性的地方。

Yes, You can override the ToString method in the MyClass to return the value of the property you want to, like 是的,您可以重写MyClass中的ToString方法以返回所需属性的值,例如

public override string ToString()
{
    return this.value;
}

With this added to your class, the last line would print you "abc" 将此添加到您的班级后,最后一行将显示“ abc”

NOTE: what happens here is that, basically when you call Console.WriteLine(<Something>) , this tries to call the ToString() method which is there in the class Object. 注意:这里发生的是,基本上,当您调用Console.WriteLine(<Something>) ,它将尝试调用类Object中的ToString()方法。 Now we know that the Object class is the Super Class of any class that we have. 现在我们知道Object类是我们拥有的任何类的超类。 So, we override the ToString() method and we return whatever string we want to return from our class. 因此,我们重写ToString()方法,并返回要从类中返回的任何字符串。

Your last line would have to be something like Console.WriteLine(obj.ToString()) . 您的最后一行必须类似于Console.WriteLine(obj.ToString())

Hope this Helps! 希望这可以帮助!

The final answer is that it cannot be done. 最终的答案是它无法完成。 A variable is a variable and an object is an object. 变量是变量,对象是对象。 Trying to go away from that and cheat will only bring other problems and I can't see how I can associate other properties to a variable without making it an object. 试图摆脱它并作弊只会带来其他问题,而且我看不到如何将其他属性关联到变量而不将其作为对象。 So it'll have to be an object and the value will be accessed with obj.value. 因此,它必须是一个对象,并且将使用obj.value访问该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM