Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze how to realize the basis of AOP Mini-frame with C #

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

It is believed that many inexperienced people are at a loss about how to analyze the basis of AOP micro-framework in C#. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Before giving a detailed introduction to the implementation of the AOP micro-framework in C #, let us first understand the .cs file of the micro-framework, and then give a comprehensive introduction to the implementation of the AOP micro-framework in C #.

In the previous series of articles, I introduced the relationship between messages, agents, and AOP, and this time I took out my own implementation of the AOP micro-framework in C # to communicate with you.

The most basic function of AOP is to implement specific pre-processing and post-processing. I let C # implement the AOP micro-framework through the agent. Let's first take a look at the .cs file that makes up the mini-framework.

1. AopProxyAttribute AOP proxy feature

Using System

Using System.Runtime.Remoting

Using System.Runtime.Remoting.Proxies

Namespace EnterpriseServerBase.Aop

{

/ / /

/ AopProxyAttribute

/ / AOP proxy feature, if a class wants to implement a specific AOP

Just implement AopProxyBase and IAopProxyFactory, and then add the feature.

/ 2005.04.11

/ / /

[AttributeUsage (AttributeTargets.Class, AllowMultiple = false)]

Public class AopProxyAttribute: ProxyAttribute

{

Private IAopProxyFactory proxyFactory = null

Public AopProxyAttribute (Type factoryType)

{

This.proxyFactory = (IAopProxyFactory) Activator.CreateInstance (factoryType)

}

# region CreateInstance

/ / /

/ / get a custom transparent proxy for the target object

/ / /

Public override MarshalByRefObject CreateInstance (Type serverType)

/ / serverType is a class decorated by AopProxyAttribute

{

/ / default transparent proxy for uninitialized instances

MarshalByRefObject target = base.CreateInstance (serverType)

/ / the initialized instance is required (ctor is not executed)

Object [] args = {target, serverType}

/ / AopProxyBase rp = (AopProxyBase) Activator.CreateInstance (this.realProxyType, args)

/ / Activator.CreateInstance passed the proxy when calling ctor, so it will fail here

/ / get a custom real agent

AopProxyBase rp = this.proxyFactory.CreateAopProxyInstance (target, serverType)

/ / new AopControlProxy (target, serverType)

Return (MarshalByRefObject) rp.GetTransparentProxy ()

}

# endregion

}

}

2 .MethodAopSwitcherAttribute.cs

Using System

Namespace EnterpriseServerBase.Aop

{

/ / /

/ MethodAopSwitcherAttribute

Used to determine whether a particular method of an AopProxyAttribute-modified class enables interception.

/ / creation reason: most of the time we only want to use interception for part of the Method of a class instead of all Method.

/ / usage: if a method does not use MethodAopSwitcherAttribute

Features or decorated with MethodAopSwitcherAttribute (false)

It will not be intercepted. Interception is enabled only if MethodAopSwitcherAttribute (true) is used.

/ 2005.05.11

/ / /

[AttributeUsage (AttributeTargets.Method, AllowMultiple = false)]

Public class MethodAopSwitcherAttribute: Attribute

{

Private bool useAspect = false

Public MethodAopSwitcherAttribute (bool useAop)

{

This.useAspect = useAop

}

Public bool UseAspect

{

Get

{

Return this.useAspect

}

}

}

}

After reading the above, have you mastered the method of how to implement the basic analysis of AOP micro-framework in C #? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report