Singleton
14 posts • Page 1 of 2 • 1, 2
Singleton
Hi! I wanna do something like this:
[Singleton]
public class Something
{
public Something()
{
... initialization ...
}
... blah blah ...
}
And when i wrote "new Something()" always get the same instance.
Is this possible? Im thinking on doing a replacement of the .ctor il to get the same instance, stored in a static field created in post compilation. Or trying to do it with Laos is possible or easier?
Ignacio Vivona
- cocotapioca
- Posts: 19
- Joined: Sat Jan 12, 2008 4:55 pm
- Full Name: Ignacio Vivona
Re: Singleton
After some time taking exams and on vacation i wanna continue with this singleton attribute. How can i add a new field (static) to a Type? There is some example a TypeAdvice? I need to treat constructors like methods (actually they are, but are called with newobject)?
Ill appreciate any help
Ill appreciate any help
Ignacio Vivona
- cocotapioca
- Posts: 19
- Joined: Sat Jan 12, 2008 4:55 pm
- Full Name: Ignacio Vivona
Re: Singleton
Actually, every time you add an aspect to a type (or a field or method of a type), you add a static field to this type: the aspect itself.
Therefore, every instance field of an aspect can be considered as a static field of the declaring type.
If you don't need to "remember" the value of that field between compile- and run-time, you should mark the field as [NonSerialized].
It is not currently possible to intercept calls to constructors, so I don't see how you could implement the Singleton pattern.
Gael
Therefore, every instance field of an aspect can be considered as a static field of the declaring type.
If you don't need to "remember" the value of that field between compile- and run-time, you should mark the field as [NonSerialized].
It is not currently possible to intercept calls to constructors, so I don't see how you could implement the Singleton pattern.
Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
got good support? consider donating to the project.
- gfraiteur
- Site Admin
- Posts: 673
- Joined: Tue Dec 18, 2007 3:09 pm
- Full Name: Gael Fraiteur
- Company: postsharp.org
Re: Singleton
Ok, thanks for the reply, but how oyu use the JointPointind.InsteadOfNewObject?
Im thinking of doing something like this:
*mark the singleton type
*create a Intance getter and a field to store the sole instance
*scan all methods/constructors for the use of newobject+ctor for singleton types
*replace that by a call to get_Instance
Im thinking of doing something like this:
*mark the singleton type
*create a Intance getter and a field to store the sole instance
*scan all methods/constructors for the use of newobject+ctor for singleton types
*replace that by a call to get_Instance
Ignacio Vivona
- cocotapioca
- Posts: 19
- Joined: Sat Jan 12, 2008 4:55 pm
- Full Name: Ignacio Vivona
Re: Singleton
Ok, thanks for the reply, but how oyu use the JointPointind.InsteadOfNewObject?
It is available only from PostSharp Core, not from PostSharp Laos. So you should write your own abstract aspect.
Gael Fraiteur, project leader
got good support? consider donating to the project.
got good support? consider donating to the project.
- gfraiteur
- Site Admin
- Posts: 673
- Joined: Tue Dec 18, 2007 3:09 pm
- Full Name: Gael Fraiteur
- Company: postsharp.org
Re: Singleton
Yes im using Postsharp.core directly, not laos. So you say i need to write my own abstract aspect, i assume that ther is not example about that. Ill dive into postsharp code.
When you use JointPointKind.InsteadOfNewObject? Is a Type o Method kind of joint point?
Have you use this feature? Can you show me some code?
Thanks for the quick reply.
When you use JointPointKind.InsteadOfNewObject? Is a Type o Method kind of joint point?
Have you use this feature? Can you show me some code?
Thanks for the quick reply.
Ignacio Vivona
- cocotapioca
- Posts: 19
- Joined: Sat Jan 12, 2008 4:55 pm
- Full Name: Ignacio Vivona
Re: Singleton
It is a method-level advice. You should specify the constructors to intercept in the operand, and null (=all methods) for the set of targets.
There is an example of how to create an aspect based on Laos: PostSharp.Samples.Librarian.
Good luck - you will have good time
.
There is an example of how to create an aspect based on Laos: PostSharp.Samples.Librarian.
Good luck - you will have good time
Gael Fraiteur, project leader
got good support? consider donating to the project.
got good support? consider donating to the project.
- gfraiteur
- Site Admin
- Posts: 673
- Joined: Tue Dec 18, 2007 3:09 pm
- Full Name: Gael Fraiteur
- Company: postsharp.org
Re: Singleton
Thanks for the reply.
I have a question, how do i get a MetadataDeclaration for the constructor .ctor from TypeDefDeclaration?
I have a question, how do i get a MetadataDeclaration for the constructor .ctor from TypeDefDeclaration?
Ignacio Vivona
- cocotapioca
- Posts: 19
- Joined: Sat Jan 12, 2008 4:55 pm
- Full Name: Ignacio Vivona
Re: Singleton
Something like;
typeDef.Methods.GetByName(".ctor")
Gel
typeDef.Methods.GetByName(".ctor")
Gel
Gael Fraiteur, project leader
got good support? consider donating to the project.
got good support? consider donating to the project.
- gfraiteur
- Site Admin
- Posts: 673
- Joined: Tue Dec 18, 2007 3:09 pm
- Full Name: Gael Fraiteur
- Company: postsharp.org
Re: Singleton
That was my first try but typeDef.Methods.GetByName return an IEnumerable<MethodDefDelcaration> and operands is a IEnumarable<MetadataDeclaration>
And cant be casted.
Cheers.
Ignacio
And cant be casted.
Cheers.
Ignacio
Ignacio Vivona
- cocotapioca
- Posts: 19
- Joined: Sat Jan 12, 2008 4:55 pm
- Full Name: Ignacio Vivona
14 posts • Page 1 of 2 • 1, 2