简体   繁体   English

在C#中是否可以访问仅具有属性值的类实例?

[英]Is it possible in c# to access class instance having only property value?

May be it's silly question , but is it possible to realize such scenario? 可能这是一个愚蠢的问题,但是有可能实现这种情况吗? I have a class , eg 我有一个课,例如

public class Car
{
public string Make {get;set;}
public string Model {get;set;}
public decimal Price {get;set;}
public User Owner {get;set;}
}

where owner is the property setting somewhere else and than i have a function 其中所有者是其他地方的属性设置,而不是我有一个功能

public static class Operations
    {
public static void CalculateTaxes(decimal price)
       {
           ....some code goes here and I want using reflection to access Owner,supposing that i will always pass price of a car
       }
    }

that function will be called like this 该函数将像这样被调用

var car =new Car(){Price=25000,Make="BMW",Model="X6",Owner=new User(){"Sam Johnson"}};
Operations.CalculateTaxes(car.Price);

This is just a fake code, in real app I don't want to pass the whole instance , but want to get hold of the car owner.Is it a real goal ? 这只是一个伪造的代码,在真实的应用程序中,我不想传递整个实例,但想要获得车主的控制权,这是一个真正的目标吗?

The answer to your question is simple and short: No. This is not possible, under any circumstance- because of how C# is designed. 您的问题的答案很简单而且很简短:不。在任何情况下,这都是不可能的,因为C#是如何设计的。 Thank Mads for that. 谢谢Mads。

Imagine the security implications of passing an integer to a compromised 3rd party library- and accidentally leaking user credentials by doing so. 想象一下将整数传递给受损的第三方库的安全隐患,并且这样做会意外泄漏用户凭据。

The real issue here is a design issue. 这里真正的问题是设计问题。 Your code is not designed to suit the needs of your application. 您的代码不适合您的应用程序的需求。 Rethink your design. 重新考虑您的设计。

Also note that while reflection has many use cases and enables a lot of things, in such simple cases- it is usually overkill. 还要注意,尽管反射有很多用例,并且可以实现很多功能,但是在这种简单的情况下,反射通常是多余的。 Use reflection for meta programming - not for business logic. 将反射用于元编程-而不用于业务逻辑。

Not only can that not be done with decimal which is a value type. 不仅不能用作为值类型的decimal来完成。 Even a reference type such as a class won't work because (AFAIK) classes do not keep track of all of the objects that point to them (The same class instance can be a property of multiple owners). 甚至类之类的引用类型也不起作用,因为(AFAIK)类无法跟踪指向它们的所有对象(同一类实例可以是多个所有者的属性)。

Fortunately there's a simple solution - pass the car instance itself, and extract the price from it. 幸运的是,有一个简单的解决方案-传递汽车实例本身,并从中提取价格。

public static void CalculateTaxes(Car car)
{
    decimal price = car.Price;
    ...
}

If you need to call this method with types other than Car , you can create an appropriate interface and have Car implement it. 如果需要使用Car以外的类型调用此方法,则可以创建一个适当的接口并让Car实现它。 Such as an interface with an Owner property. 例如具有Owner属性的接口。 But since you're trying to get more properties of the car, I'm assuming this is going to be used only for type Car . 但是,由于您试图获得更多的汽车属性,因此我假设这将仅用于Car类型。

I thought of that solution ,but it's now what I want,as there will be a lot of properties of different type and each of them should be process in a different function and I want to pass them using dot notation ,not like CalculateTax(car,"Price") 我想到了该解决方案,但现在正是我想要的,因为会有很多不同类型的属性,并且每个属性都应在不同的函数中进行处理,而我想使用点表示法传递它们,而不是像CalculateTax(car,"Price")

I think , that the best solution will be to use local functions, which will have access to the whole object like 我认为最好的解决方案是使用局部函数,该函数可以访问整个对象,例如

public static class Operations
    {
    public static void Operate(Car car)
       {
           calculateTaxes(car.Price);
         void calculateTaxes(decimal price)
         { 
          car.Owner is accessible here 
          }

         void rateMake(string make)
         { 
          so on and so on
          }


       }

    }

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

相关问题 是否可以使用 class 实例作为 C# 中的值? - Is it possible to use a class instance as a value in C#? c#从类的实例中获取静态属性的值 - c# Get value of static property from the instance of a class 如何从C#中的diffrrent类访问实例变量值 - How to access the instance variable value from the diffrrent class in C# 在c#中,是否有可能在不知道包含类的情况下从该属性的实例中获取附加到该属性的属性? - Is it possible in c# to get the attributes attached to a property from within the instance of that property, without knowing the containing class? 类实例属性可以作为属性中的值 - Possible for a class instance property as a value in an attribute 如何从子类C#的实例访问基类属性 - How to access a base class property from instance of child class C# C#访问动态类实例字段 - C# Access dynamic class instance field C#读取字符串中的类树路径,并在给定该类实例的情况下访问值 - C# read a class tree path in a string and access a value given an instance of that class 这在c#中可能吗? 只有一个类可以访问基类方法,而第二个类不能访问相同的基类方法 - is this possible in c# ? only one class access base class method and second class can not access the same base class method 是否有可能通过c#中的值获取属性 - is it possible to get a property by its value in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM