[英]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.