Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 使用介绍的成员?_C# 4.0_Postsharp - Fatal编程技术网

C# 4.0 使用介绍的成员?

C# 4.0 使用介绍的成员?,c#-4.0,postsharp,C# 4.0,Postsharp,我一直在阅读PostSharp,我想用简介来注入一名成员。举个愚蠢的例子,假设我写了一个方面 public Guid Id = Guid.NewGuid(); 很简单,如果我拆开,我会看到它在那里。现在是棘手的部分。。在同一个班级里,我想做一些事情,比如 public void PrintGuid() { Console.WriteLine(this.Id); } 我该怎么做?或者,如果这不起作用,我可以在类中声明变量,然后在方面中使用它吗?所以全班都会 public Guid I

我一直在阅读PostSharp,我想用简介来注入一名成员。举个愚蠢的例子,假设我写了一个方面

public Guid Id = Guid.NewGuid();
很简单,如果我拆开,我会看到它在那里。现在是棘手的部分。。在同一个班级里,我想做一些事情,比如

public void PrintGuid()
{
     Console.WriteLine(this.Id);
}
我该怎么做?或者,如果这不起作用,我可以在类中声明变量,然后在方面中使用它吗?所以全班都会

public Guid Id;
这个相位会做一些类似的事情

this.Id = Guid.NewGuid();

你可以在这里看到怎么做

如果您需要在设计时使用它,那么您可能不应该在编译时引入它。将其更改为在设计时存在,或者可以抽象接口并使用Post.Cast()来利用它

var MyClass = Post.Cast<IMyInterface>(...);

MyClass.MyIntroducedMember = 10;
var MyClass=Post.Cast(…);
MyClass.MyIntroductedMember=10;

您在设计时找到了这样做的方法吗?