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

What are the dynamic agent modes and Mixin modes?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what dynamic agent mode and Mixin mode". In daily operation, I believe that many people have doubts about what dynamic agent mode and Mixin mode there are. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what dynamic agent mode and Mixin mode"! Next, please follow the editor to study!

Heavyweight ORM and IOC products are inseparable from dynamic agents, as developers, most of the time do not need to pay attention to the internal implementation mechanism of dynamic agents, but it is necessary to understand its general rules and patterns, for example: although you use POCO during development, because dynamic agents are turned on, it is not POCO during operation. This paper briefly describes five kinds of agent generation mode and one kind of Mixin mode, and finally gives an example.

The copy code is as follows:

Public interface IPlayable

{

Void Play ()

}

Public class Animal: IPlayable

{

Public virtual void Play ()

{

Console.WriteLine ("Animal.Play")

}

}

Public class Dog: Animal

{

Public override void Play ()

{

Console.WriteLine ("Dog.Play")

}

}

Public interface IRunable

{

Void Run ()

}

Public class RunAbility: IRunable

{

Public void Run ()

{

Console.WriteLine ("RunAbility.Run")

}

}

Public class AnimalInterceptor: IInterceptor

{

Public void Intercept (IInvocation invocation)

{

Console.WriteLine ("Before AnimalInterceptor.Intercept")

If (invocation.InvocationTarget! = null)

{

Invocation.Proceed ()

}

Console.WriteLine ("After AnimalInterceptor.Intercept")

}

}

The first kind: ClassProxy

The copy code is as follows:

{

Console.WriteLine ("\ nClassProxy*\ n")

Var generator = new ProxyGenerator ()

Var animal = generator.CreateClassProxy (new AnimalInterceptor ())

Animal.Play ()

Console.WriteLine (animal.GetType ())

Console.WriteLine (animal.GetType () .BaseType)

Var compositeField = animal.GetType () .GetField ("_ _ target")

Console.WriteLine (compositeField)

Foreach (var interfaceType in animal.GetType () .GetInterfaces ())

{

Console.WriteLine (interfaceType)

}

}

The second kind: ClassProxyWithTarget

The copy code is as follows:

{

Console.WriteLine ("\ nClassProxyWithTarget*\ n")

Var generator = new ProxyGenerator ()

Var animal = generator.CreateClassProxyWithTarget (new Dog (), new AnimalInterceptor ())

Animal.Play ()

Console.WriteLine (animal.GetType ())

Console.WriteLine (animal.GetType () .BaseType)

Var compositeField = animal.GetType () .GetField ("_ _ target")

Console.WriteLine (compositeField)

Foreach (var interfaceType in animal.GetType () .GetInterfaces ())

{

Console.WriteLine (interfaceType)

}

}

The third kind: InterfaceProxyWithoutTarget

The copy code is as follows:

{

Console.WriteLine ("\ nProxyWithoutTarget *\ n")

Var generator = new ProxyGenerator ()

Var animal = generator.CreateInterfaceProxyWithoutTarget (new AnimalInterceptor ())

Animal.Play ()

Console.WriteLine (animal.GetType ())

Console.WriteLine (animal.GetType () .BaseType)

Var compositeField = animal.GetType () .GetField ("_ _ target")

Console.WriteLine (compositeField)

Foreach (var interfaceType in animal.GetType () .GetInterfaces ())

{

Console.WriteLine (interfaceType)

}

}

The fourth kind: InterfaceProxyWithTarget

The copy code is as follows:

{

Console.WriteLine ("\ nProxyWithTarget *\ n")

Var generator = new ProxyGenerator ()

Var animal = generator.CreateInterfaceProxyWithTarget (new Dog (), new AnimalInterceptor ())

Animal.Play ()

Console.WriteLine (animal.GetType ())

Console.WriteLine (animal.GetType () .BaseType)

Var compositeField = animal.GetType () .GetField ("_ _ target")

Console.WriteLine (compositeField)

Foreach (var interfaceType in animal.GetType () .GetInterfaces ())

{

Console.WriteLine (interfaceType)

}

}

The fifth kind: InterfaceProxyWithTargetInterface

The copy code is as follows:

{

Console.WriteLine ("\ nProxyProxyTargetInterface *\ n")

Var generator = new ProxyGenerator ()

Var animal = generator.CreateInterfaceProxyWithTargetInterface (new Dog (), new AnimalInterceptor ())

Animal.Play ()

Console.WriteLine (animal.GetType ())

Console.WriteLine (animal.GetType () .BaseType)

Var compositeField = animal.GetType () .GetField ("_ _ target")

Console.WriteLine (compositeField)

Foreach (var interfaceType in animal.GetType () .GetInterfaces ())

{

Console.WriteLine (interfaceType)

}

}

Mixin mode

The copy code is as follows:

{

Console.WriteLine ("\ nmix *\ n")

Var generator = new ProxyGenerator ()

Var options = new ProxyGenerationOptions ()

Options.AddMixinInstance (new RunAbility ())

Var animal = generator.CreateClassProxy (options, new AnimalInterceptor ())

Animal.Play ()

(animal as IRunable) .Run

Console.WriteLine (animal.GetType ())

Console.WriteLine (animal.GetType () .BaseType)

Var compositeField = animal.GetType () .GetField ("_ _ target")

Console.WriteLine (compositeField)

Foreach (var field in animal.GetType () .GetFields ())

{

If (field.Name.StartsWith ("_ _ mixin"))

{

Console.WriteLine (field)

}

}

Foreach (var interfaceType in animal.GetType () .GetInterfaces ())

{

Console.WriteLine (interfaceType)

}

}

At this point, the study on "what are the dynamic agent patterns and Mixin patterns" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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