I only want this invoked when a property is set. Why is this not working?
[DirtyTrackingAttribute(AttributeTargetElements =
PostSharp.Extensibility.MulticastTargets.Property)]
class Program
{
public static string Test { get; set; }
static void Main(string[] args)
{
TestIt();
Test = "foo";
Console.ReadKey();
}
private static void TestIt()
{
Console.WriteLine("Real method called");
}
}
[Serializable]
public class DirtyTrackingAttribute : OnMethodInvocationAspect
{
public override void OnInvocation(MethodInvocationEventArgs eventArgs)
{
Console.WriteLine("Property invoked");
eventArgs.Proceed();
}
}
If you want the aspect to be applied on property setters only, you can filter the method name with the expression "set_*":
[DirtyTrackingAttribute(AttributeTargetMembers="set_*")]
PostSharp 1.* does not support explicitely properties; property accessors are considered as plain methods.
Related
Here is the code I currently have, the question follows after:
class Program
{
static void Main(string[] args)
{
var obj1 = new A();
obj1.DoIt();
obj1.SetFlyBehavior(new BehaviorB());
obj1.DoIt();
string input = Console.ReadLine();
}
};
class BaseOfA
{
protected ObjectBehavior behavior;
public void DoIt()
{
behavior.DoIt();
}
public void SetBehavior(ObjectBehavior ob) {
behavior = ob;
}
};
class A : BaseOfA {
public A() {
behavior = new BehaviorA();
}
}
interface ObjectBehavior {
void DoIt();
}
class BehaviorA : ObjectBehavior {
void ObjectBehavior.DoIt() {
Console.WriteLine("Behavior: A");
}
}
class BehaviorB : ObjectBehavior {
void ObjectBehavior.DoIt() {
Console.WriteLine("Behavior: B");
}
}
Now my question is, in this case, how am I going to make it work so that I can assign both BehaviorA and BehaviorB to instance obj1 as long as they implement ObjectBehavior?
You are calling obj.SetFlyBehaviour this method is not defined anywhere. The method you define on BaseOfA is called SetBehaviour. Once that is fixed the code you gave compiles fine for me
I'm playing with VS. I'm a rookie, It must be basic. I have created 2 classes and I puzzled with result. I'm using Visual Studio 2015 community edition.
I'm expect to receive at console :
myfirstClass
Class
first.
mysecondClass
Class
second.
I received :
myfirstClass
Class
_
class Program
{
public class mysecondClass
{
static public string myName ;
public mysecondClass()
{
Console.WriteLine("mysecondClass");
myName = "Class";
}
public static void Display()
{
Console.WriteLine(myName);
}
~mysecondClass()
{
Console.WriteLine("second.");
}
}
public class myfirstClass
{
public string myName;
public myfirstClass()
{
Console.WriteLine("myfirstClass");
myName = "Class";
}
public static void Display()
{
myfirstClass d = new Program.myfirstClass();
Console.WriteLine(d.myName);
}
~myfirstClass()
{
Console.WriteLine("first.");
}
}
static void Main(string[] args)
{
myfirstClass.Display();
mysecondClass.Display();
Console.ReadLine();
}
}
This is not a bug in Visual Studio. I think you have got two concepts wrongly.
Firstly, the finaliser of a class will not be called immediately after the object is out of scope. It will be called at a random time. It is quite unpredictable.
Therefore, this:
Console.WriteLine("first.");
is not printed.
The second thing is that constructors of a class is only called when you write new XXX(...) (or through reflection). Just calling a static method will not invoke the constructor.
In other words, these lines are never executed:
Console.WriteLine("mysecondClass");
myName = "Class";
You never wrote new mysecondClass().
When this line in the display method of mysecondClass executes:
Console.WriteLine(myName);
Since myName has not been assigned, it is null, and so nothing is printed.
class Program
{
public class mysecondClass
{
public string myName { get; set; }
public mysecondClass()
{
Console.WriteLine("mysecondClass");
myName = "Class";
}
public static void Display()
{
var mySecond = new mysecondClass();
Console.WriteLine(mySecond.myName);
}
~mysecondClass()
{
Console.WriteLine("second.");
}
}
public class myfirstClass
{
public string myName { get; set; }
public myfirstClass()
{
Console.WriteLine("myfirstClass");
myName = "Class";
}
public static void Display()
{
myfirstClass d = new myfirstClass();
Console.WriteLine(d.myName);
}
~myfirstClass()
{
Console.WriteLine("first.");
}
}
static void Main(string[] args)
{
myfirstClass.Display();
mysecondClass.Display();
Console.ReadLine();
}
}
try this
I have a method like this
public void MyMethod(MyType parameter) { /* ... */ }
I'd like to force a value into one of parameter's public property before the method gets excecuted.
It must happen before method excecution because some postSharp OnMethodBoundaryAspect.OnEntry depends on this property's value.
An ideal solution could looks like this:
[SetPropertyBeforeEntry(0 /* Argument's index*/, nameof(MyType.MyProperty) /* Property to set */, 1234 /* Value */)]
public void MyMethod(MyType parameter) { /* ... */ }
You have two options - OnMethodBoundaryAspect and MethodInterceptionAspect, but in both cases you need to use aspect dependencies to make sure you have proper order (PostSharp would produce warnings if the order is not specified).
PostSharp provides various ways to specify dependencies between aspects. You can find more here.
The following demonstrates changing of the parameter's property before (without the specific logic for argument type, property or index.
class Program
{
static void Main(string[] args)
{
new TestClass().Foo(new TestData());
}
}
public class TestClass
{
[ObservingAspect]
[ArgumentChangingAspect]
public int Foo(TestData data)
{
Console.WriteLine("Method observed: {0}", data.Property);
return data.Property;
}
}
public class TestData
{
public int Property { get; set; }
}
[AspectTypeDependency(AspectDependencyAction.Order, AspectDependencyPosition.Before, typeof(ObservingAspect))]
[PSerializable]
public class ArgumentChangingAspect : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
((TestData) args.Arguments[0]).Property = 42;
}
}
[PSerializable]
public class ObservingAspect : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
Console.WriteLine("Aspect observed: {0}", ((TestData)args.Arguments[0]).Property);
}
}
The code is simple enough to understand I hope.
I'm trying to use an interface type IColor in order to pass color objects to the ColorManager. I then want the ColorManager to pass this object to the IColor object as its own type, so the method overloads gets called.
However, it seems since it is being passed as the IColor type, C# will not implicity cast it into its complete type as either a BlueColor or GreenColor.
I hope this makes some sense to somebody on what I want to achieve. Is this possible in C#?
[Solution]
http://msdn.microsoft.com/en-us/library/dd264736.aspx
Overload Resolution with Arguments of Type dynamic
My code so far:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
namespace Example
{
public interface IColor
{
void CatchColor(IColor c);
}
public class BlueColor : IColor
{
public void CatchColor(IColor c)
{
}
}
public class GreenColor : IColor
{
public void CatchColor(BlueColor c)
{
Console.WriteLine("CAUGHT BLUE!");
}
public void CatchColor(GreenColor c)
{
Console.WriteLine("CAUGHT GREEN!");
}
public void CatchColor(IColor c)
{
Console.WriteLine("CAUGHT SOME COLOR!");
}
}
public class ColorManager
{
public void PassColor(IColor c)
{
// Don't use static type-checking
// Problem solved
dynamic AnyColor = c;
AnyColor.CatchColor(AnyColor);
}
public static void Main()
{
GreenColor G = new GreenColor();
new ColorManager().PassColor(G);
Console.ReadLine();
return;
}
}
}
One possiblity to tell the ColorManager class to use the correct type of the passed object is to use an abstract class, that already implements the CatchColor:
public abstract class IColor
{
// override in every class
public abstract void PrintColor();
// has the correct type passed with the interface
public void CatchColor(IColor c)
{
c.PrintColor();
}
}
Then the sub classes need to implement only PrintColor with the correct color:
public class BlueColor : IColor
{
public override void PrintColor()
{
Console.WriteLine("BLUE!");
}
}
public class GreenColor : IColor
{
public override void PrintColor()
{
Console.WriteLine("GREEN!");
}
}
The manager is the same:
public class ColorManager
{
public void PassColor(IColor c)
{
c.CatchColor(c);
}
}
It can be used like this:
GreenColor G = new GreenColor();
var cm = new ColorManager();
cm.PassColor(G);
cm.PassColor(new BlueColor());
The outputs is:
GREEN!
BLUE!
What you want is late method binding.
The downside to this is you have to add methods for each new type of color. The upside is you don't have to maintain a case statement or conditional logic.
See here for more detail:
Early and late binding
Edit: Here is a working example of this type of late-binding.
class Program {
static void Main(string[] args) {
//Declare instances
BaseClass myClass = new Class2();
BaseClass otherClass = new Class1();
//Invoke the action method which will match based on the BaseClass type
Action(myClass);
Action(otherClass);
Console.ReadLine();
}
public static void Action(BaseClass classType) {
//Remove the compile-time type so the runtime can select the method based on signature
dynamic aClass = classType;
ServiceMethod(aClass);
}
public static void ServiceMethod(dynamic input) {
Methods(input);
}
public static void Methods(Class1 classType) {
Console.WriteLine("Class1");
Debug.WriteLine("Class1");
}
public static void Methods(Class2 classtype) {
Console.WriteLine("Class2");
Debug.WriteLine("Class2");
}
public static void Methods(Class3 classType) {
Console.WriteLine("Class3");
Debug.WriteLine("Class3");
}
}
public abstract class BaseClass { //This could also be an interface
public Guid Id { get; set; }
public string Name { get; set; }
}
public class Class1 : BaseClass {
}
public class Class2 : BaseClass{
}
public class Class3 : BaseClass {
}
So you want something like:
public void CatchColor(Color c)
{
if (c is BlueColor)
CatchColor(c as BlueColor);
if (c is GreenColor)
CatchColor(c as GreenColor);
}
?
I am running a .NET 4.0 Web Application (not web site) and PostSharp 1.5. I cannot get the OnEntry override method to execute using the OnMethodBoundaryAspect base class. Here is some relevant code:
public sealed class MonitorAttribute : OnMethodBoundaryAspect {
public string[] SomeValue { get; protected set; }
public MonitorAttribute (params string[] someValue){
SomeValue = someValue;
}
public override void OnEntry(MethodExecutionEventArgs eventArgs){
// do Something here
base.OnEntry(eventArgs);
}
}
public sealed class MyUsageClass : IMyUsageClass {
[Monitor(new string[]{ 'Test' })
public void SomeMethod {
// Do something else in here
}
}
Am I missing something? It never hits the OnEntry method. I also tried replacing my PostSharp.dll and PostSharp.Laos.dll dependencies with the new 2.0 version. If it makes any difference MyUsageClass is instantiated by StructureMap.
Yes, every dev will need to have PostSharp installed. If you're just using the starter edition then it's all free.
Posting this as an answer to show you the code. My test code
class Program
{
[Monitor]
static void Main(string[] args)
{
}
}
[Serializable]
public class MonitorAttribute : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
Console.WriteLine("OnEntry");
}
}
The code after compilation
internal class Program
{
[CompilerGenerated, DebuggerNonUserCode]
internal sealed class <>z__Aspects
{
internal static MethodBase m1 = MethodBase.GetMethodFromHandle(ldtoken(Main()));
internal static readonly MonitorAttribute a0 = (MonitorAttribute)<>z__AspectsImplementationDetails.aspects1[0];
}
private static void Main(string[] args)
{
Program.<>z__Aspects.a0.OnEntry(null);
}
}