简体   繁体   中英

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?

Instead of writing Object.ObjectTwo.Property = something , I would like to be able to write 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; ).

If you want to shorten Object.ObjectTwo to something simpler, you can use a using statement in the following manner:

using Object.ObjectTwo = ObjTwo;

Then, you can later call ObjTwo.Variable = someVar; , and it will act as if you had used Object.ObjectTwo.Variable = someVar;

In C# 7, you can use 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.

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.

How about:

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

The example camera.backgroundColor.r = 1 from the comment simply won't work. You will get the following error

Cannot modify the return value of 'Camera.backgroundColor' because it is not a variable

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.


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. 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.

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. Otherwise, you could break a link somewhere.

Use a local function (or on C# versions < 7.0 a delegate).

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

    Shortcut(value1);
    Shortcut(value2);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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