public class Kitten
{
public string Ears = "NotSet";
public string Fur = "NotSet";
}
public class Cage
{
Kitten _kitten;
public Cage(Kitten Akitten)
{
}
public Kitten Kitten
{
get
{
return _kitten;
}
set
{
_kitten = value;
}
}
public string EchoKittenProperty(string PropName)
{
return PropName;
}
}
有没有办法获得对新类的访问权并将其吐入内联方法中?
这很容易:
var cg = new Cage( new Kitten(){ Ears = "Pointy", Fur = "Fluffy" });
var value = cg.EchoKittenProperty(cg.Kitten.Fur);
//value = "Fluffy"
但是我该怎么做:
var value = new Cage( new Kitten(){ Ears = "Pointy", Fur = "Fluffy" }).EchoKittenProperty(this.Kitten.Fur);
//value = "Fluffy"
这是一个测试班:
public static class Test
{
public static void Test1()
{
var cg = new Cage( new Kitten(){ Ears = "Pointy", Fur = "Fluffy" });
var value = cg.EchoKittenProperty(cg.Kitten.Fur);
//value = "Fluffy"
}
public static void Test2()
{
var value = new Cage( new Kitten(){ Ears = "Pointy", Fur = "Fluffy"}).EchoKittenProperty(this.Kitten.Fur);
//value = "Fluffy"
}
}
是否有任何方法可以内联引用EchoKittenProperty方法中的新对象,而无需将其放入变量中并在之后引用它?
您需要将整个新对象放在方括号()中。 “新”也必须放在方括号中。
然后您将可以在该对象上调用实例方法:
( new Cage(new Kitten(){ Ears = "Pointy", Fur = "Fluffy"}) )
.EchoKittenProperty(this.Kitten.Fur);