如何缩短前往物业的路径?

时间:2019-02-15 14:26:24

标签: c#

例如,如果我有Object.ObjectTwo.Property,并且我不想一直写代码而使代码混乱,是否有办法使代码更短?

我希望能够写Object.ObjectTwo.Property = something,而不是写myVariable = something

尝试搜索时找不到任何东西。

编辑:有问题的成员是一个属性。

7 个答案:

答案 0 :(得分:3)

也许只是声明一个单独的变量?

var ObjectA = Object.ObjectTwo.Variable;

尽管这对您来说更方便,但在计算机端,它是另一个声明的变量。

答案 1 :(得分:3)

C#中,您可以在全局范围(在其中放置诸如using System;之类的语句)中为变量类型创建简写。

如果您想将Object.ObjectTwo简化为更简单的内容,则可以通过以下方式使用using语句:

using Object.ObjectTwo = ObjTwo;

然后,您以后可以调用ObjTwo.Variable = someVar;,它将像您曾经使用过Object.ObjectTwo.Variable = someVar;

答案 2 :(得分:3)

在C#7中,您可以使用Ref Locals。与大多数其他方法不同,即使对结构进行操作,该方法也可以安全使用。

此方法仅适用于字段。不能使用ref作为别名。

下面是一个例子。

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)
}

答案 3 :(得分:2)

通过实现可能使您更接近目标的“捷径”,可以给自己一些语法糖。

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

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

这允许您编写例如:

var one = new ObjectOne();

one.Var = something;

@Eric Lippert是对的,这只是问题需要更多信息才能正确回答的一种可能解决方案。

答案 4 :(得分:0)

怎么样:

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

答案 5 :(得分:0)

comment中的示例camera.backgroundColor.r = 1根本无法工作。您将收到以下错误

  

无法修改'Camera.backgroundColor'的返回值,因为它不是变量

,并且已经讨论了原因here。关键是在Unity中,Camera本身就是class,但是ColorbackgoundColor的类型)是可变的struct,要小心,尽管它们是邪恶的。


  

将新值分配给值类型的变量时,该值   被复制。将新值分配给引用的变量时   类型,引用被复制,而不是对象本身

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

value类型是按值复制的,因此shortCutValuecamera.backgroundColor.r没有任何关系,只不过它们在存在的一个时期内具有相同的值。另一方面,shortCutRef是实际的快捷方式,它将起作用,直到您更改对backgroundColorRef的引用,如果Camera是可变的,则可能是可行的。

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

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

我不确定这是否通常适用,也许埃里克知道某些情况,但是如果您拥有A.B.C.D....N.r,并且实际上Nreference类型,则可以创建一个快捷方式并确保带有快捷方式的链接是不可破坏的,从N到A的所有类型都应该是不可变的。否则,您可能会在某处断开链接。

答案 6 :(得分:0)

使用本地函数(或在C#版本<7.0上为委托)。

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

    Shortcut(value1);
    Shortcut(value2);
}
相关问题