简体   繁体   English

如何缩短物业的路径?

[英]How do I shorten the path to a property?

For example if I have Object.ObjectTwo.Property and I don't want to clutter my code by writing that all the time, is there a way to make it shorter? 例如,如果我有Object.ObjectTwo.Property并且我不想通过一直写下来混乱我的代码,有没有办法缩短它?

Instead of writing Object.ObjectTwo.Property = something , I would like to be able to write myVariable = something . 而不是写Object.ObjectTwo.Property = something ,我希望能够写myVariable = something

I couldn't find anything when I tried searching. 我尝试搜索时找不到任何东西。

Edit: The member in question is a property. 编辑:有问题的成员是一个属性。

Maybe just declare a separate variable? 也许只是声明一个单独的变量?

var ObjectA = Object.ObjectTwo.Variable;

Though while this is more convenient for you, on the computer side, it is one more declared variable. 虽然这对你来说更方便,但在计算机方面,它是另一个声明的变量。

In C# , you can create shorthands for variable types at the global scope (where you put statements like using System; ). C#中 ,您可以在全局范围内创建变量类型的缩写(您可以在其中放置语句,例如using System; )。

If you want to shorten Object.ObjectTwo to something simpler, you can use a using statement in the following manner: 如果要将Object.ObjectTwo缩短为更简单的东西,可以按以下方式使用using语句:

using Object.ObjectTwo = ObjTwo;

Then, you can later call ObjTwo.Variable = someVar; 然后,你可以稍后调用ObjTwo.Variable = someVar; , and it will act as if you had used Object.ObjectTwo.Variable = someVar; ,它就好像你曾经使用过Object.ObjectTwo.Variable = someVar;

In C# 7, you can use Ref Locals . 在C#7中,您可以使用Ref Locals Unlike most other approaches, this approach can be used safely even when operating on structs. 与大多数其他方法不同,即使在结构上运行,也可以安全地使用此方法。

This approach is only available on fields. 此方法仅适用于字段。 Properties cannot be aliased using ref. 使用ref不能对属性进行别名。

Below is an example. 以下是一个例子。

struct bar
{
    public int myprop;
}

struct bash 
{
    public bar mybar;
}

void Main()
{
    bash bash1 = new bash();
    bash1.mybar.myprop = 1;
    Console.WriteLine(bash1.mybar.myprop); //Outputs 1 (Direct access)

    bar bar2 = bash1.mybar;
    bar2.myprop = 2;
    Console.WriteLine(bash1.mybar.myprop); //Outputs 1 (Bug: access via a copy)

    ref bar bar3 = ref bash1.mybar;
    bar3.myprop = 3;
    Console.WriteLine(bash1.mybar.myprop); //Outputs 3 (Ref Local)

    bar3 = new bar();
    bar3.myprop = 4;
    Console.WriteLine(bash1.mybar.myprop); //Outputs 4 (Ref local with assignment)
}

You can give yourself some syntactic sugar by implementing "shortcuts" that might get you closer to your goal. 你可以通过实施可以让你更接近目标的“快捷方式”给自己一些语法糖。

public class ObjectOne
{
    public ObjectTwo ObjectTwo {get;set;}

    public VariableType Var {get{return ObjectTwo.Variable;}}
}

This allows you to write for example: 这允许你写例如:

var one = new ObjectOne();

one.Var = something;

@Eric Lippert is right, this is only one possible solution the Question needs more information to be answered correctly. @Eric Lippert是对的,这只是一个可能的解决方案,问题需要更多信息才能正确回答。

How about: 怎么样:

var shortCut = Object.ObjectTwo;    
shortCut.Variable  = something;

The example camera.backgroundColor.r = 1 from the comment simply won't work. 注释中的示例camera.backgroundColor.r = 1将无法正常工作。 You will get the following error 您将收到以下错误

Cannot modify the return value of 'Camera.backgroundColor' because it is not a variable 无法修改'Camera.backgroundColor'的返回值,因为它不是变量

and the reason has been discussed here . 这里讨论了原因。 The point is that in Unity the Camera itself is the class but the Color (the type of the backgoundColor ) is mutable struct , be careful though they are evil. 关键是在UnityCamera本身就是classColorbackgoundColor的类型)是可变struct ,虽然它们是邪恶的但要小心。


When you assign a new value to a variable of a value type, that value is copied. 将新值分配给值类型的变量时,将复制该值。 When you assign a new value to a variable of a reference type, the reference is copied, not the object itself 将新值分配给引用类型的变量时,将复制引用,而不是对象本身

public class Camera
{
    public BackgroundColorValue backgroundColorValue { get; set; } 
        = new BackgroundColorValue();
    public BackgroundColorRef backgroundColorRef { get; set; } 
        = new BackgroundColorRef();
}

public struct BackgroundColorValue
{
    public int r { get; set; }
    public int g { get; set; }
    public int b { get; set; }
}

public class BackgroundColorRef
{
    public int r { get; set; }
    public int g { get; set; }
    public int b { get; set; }
}

var shortCutValue = cammera.backgroundColorValue;
var shortCutRef = cammera.backgroundColorRef;

shortCutValue.r = 5;
shortCutRef.r = 10;

//cammera.backgroundColorValue.r == 0, shortCutValue == 5
//cammera.backgroundColorRef.r == 10, shortCutValue == 10

The value types are copied by value so shortCutValue doesn't have any connection with the camera.backgroundColor.r except they have the same value in one period of their existance. value类型是按值复制这样shortCutValue没有与任何连接camera.backgroundColor.r除非他们在它们的存在一个周期具有相同的值。 On the other hand, shortCutRef is an actual shortcut and it will work until you change the reference to the backgroundColorRef which might be possible if Camera is mutable. 另一方面, shortCutRef是一个实际的快捷方式,它将一直有效,直到你更改对backgroundColorRef的引用,如果Camera是可变的,这可能是可能的。

var shortCutRef = cammera.backgroundColorRef;
camera.backgroundColorRef = new BackgroundColorRef(); //link to shortcut broken
shortCutRef.r = 10;

//cammera.backgroundColorRef.r == 0, shortCutValue == 10

I am not sure if this is applicable in general, there might be some case Eric knows, but if you have ABCD...Nr you could make a shortcut if N is actually reference type and to be sure the link with the shortcut will be unbreakable all types from N to A should be immutable. 我不确定这是否适用于一般,可能有一些案例Eric知道,但如果你有ABCD...Nr你可以创建一个快捷方式,如果N实际上是reference类型,并确保与快捷方式的链接将是从N到A的所有类型都应该是不可变的。 Otherwise, you could break a link somewhere. 否则,你可能会破坏某个地方的链接。

Use a local function (or on C# versions < 7.0 a delegate). 使用本地函数(或在C#版本<7.0一个委托)。

public void DoWork(SomeType thing, PropertyType value1, PropertyType value2)
{
    void Shortcut(PropertyType value) => thing.ThingTwo.Property = value;

    Shortcut(value1);
    Shortcut(value2);
}

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

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