473,748 Members | 4,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use Custom Attribute on ASSEMBLY scope

In my project, I need to use custom attribute on all assemblies. This
attribute will implement some non-functional code and I want to use it on
assembly scope(Attribute Targets is Assembly).
I add code "[assembly: CallTracing()]" in file AssemblyInfo.cs . But it
seemed that it didn't work. I don't know why.
CODE AS BELOW:

TestAttribute.c s++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ Â*++++++++++
using System;
using System.Reflecti on;
using System.Runtime. Remoting.Messag ing;
using System.Runtime. Remoting.Contex ts;
using System.Runtime. Remoting.Activa tion;
namespace AOP.Experiment
{
internal class CallTracingAspe ct : IMessageSink
{
private IMessageSink m_next;
private String m_typeAndName ;
internal CallTracingAspe ct(IMessageSink next)
{
// Cache the next sink in the chain
m_next = next;
}
public IMessageSink NextSink
{
get
{
return m_next;
}
}
public IMessage SyncProcessMess age(IMessage msg)
{
Preprocess(msg) ;
IMessage returnMethod =
m_next.SyncProc essMessage(msg) ;
PostProcess(msg , returnMethod);
return returnMethod;
}
public IMessageCtrl AsyncProcessMes sage(IMessage msg,
IMessageSink
replySink)
{
throw new InvalidOperatio nException() ;
}
public static string ContextName
{
get
{
return "CallContex t" ;
}
}
private void Preprocess(IMes sage msg)
{
if (!(msg is IMethodMessage) ) return;
IMethodMessage call = msg as IMethodMessage;
Type t = Type.GetType(ca ll.TypeName) ;
m_typeAndName = t.Name + "." + call.MethodName ;
Console.Write(" calltrace PreProcessing: " +
m_typeAndName + "(");
for (int i = 0; i < call.ArgCount; ++i)
{
if (i > 0) Console.Write(" , ");
Console.Write(c all.GetArgName( i) + "= " +
call.GetArg(i)) ;
}
Console.WriteLi ne(")");
// set us up in the callContext
call.LogicalCal lContext.SetDat a(ContextName, this);
}
private void PostProcess(IMe ssage msg, IMessage msgReturn)
{
// We only want to process method return calls
if (!(msg is IMethodMessage) ||
!(msgReturn is IMethodReturnMe ssage)) return;
IMethodReturnMe ssage retMsg =
(IMethodReturnM essage)msgRetur n;
Console.Write(" calltrace PostProcessing: ");
Exception e = retMsg.Exceptio n;
if (e != null)
{
Console.WriteLi ne("Exception was thrown: " +
e);
return;
}
// Loop through all the [out] parameters
Console.Write(m _typeAndName + "(");
if (retMsg.OutArgC ount > 0)
{
Console.Write(" out parameters[");
for (int i = 0; i < retMsg.OutArgCo unt; ++i )
{
if (i > 0) Console.Write(" , ");

Console.Write(r etMsg.GetOutArg Name(i) + "= " +
retMsg.GetOutAr g(i));
}
Console.Write("]");
}
if (retMsg.ReturnV alue.GetType() != typeof(void))
Console.Write(" returned [" +
retMsg.ReturnVa lue + "]");
Console.WriteLi ne(")");
}
}
public class CallTracingProp erty : IContextPropert y,
IContributeServ erContextSink
{
public IMessageSink GetServerContex tSink(IMessageS ink next)
{
Console.WriteLi ne("***In CallTracingProp erty's
GetObjectSink** *");
Console.WriteLi ne("Next Message Sink is " +
next.GetType()) ;
return new CallTracingAspe ct(next);
}
public bool IsNewContextOK( Context newCtx )
{
return true ;
}
public void Freeze(Context newContext)
{
}
public string Name
{
get
{
return "CallTracingPro perty";
}
}
}
[AttributeUsage( AttributeTarget s.Assembly)]
public class CallTracingAttr ibute : ContextAttribut e
{
public CallTracingAttr ibute() : base("CallTraci ng") {}
public override void
GetPropertiesFo rNewContext(ICo nstructionCallM essage ccm)
{
ccm.ContextProp erties.Add(new CallTracingProp erty());
Console.WriteLi ne("CallTracing Attribute has been
created for new
context");
}
}

}
TestClass.cs+++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +
using System;
namespace AOP.Experiment
{
public class cTestClass:Cont extBoundObject
{
public cTestClass()
{
Console.WriteLi ne("TestClass ctor()");
}
public override string ToString()
{
return "Test Class";
}
}

}
TestClass.csçš„ AssemblyInfo.cs +++++++++++++++ +++++++++++++++ +++++++
using System.Reflecti on;
using System.Runtime. CompilerService s;
using AOP.Experiment;
[assembly: CallTracing()]
[assembly: AssemblyTitle(" ")]
[assembly: AssemblyDescrip tion("")]
[assembly: AssemblyConfigu ration("")]
[assembly: AssemblyCompany ("")]
[assembly: AssemblyProduct ("")]
[assembly: AssemblyCopyrig ht("")]
[assembly: AssemblyTradema rk("")]
[assembly: AssemblyCulture ("")]
[assembly: AssemblyVersion ("1.0.*")]
[assembly: AssemblyDelaySi gn(false)]
[assembly: AssemblyKeyFile ("")]
[assembly: AssemblyKeyName ("")]
TestManager.cs+ +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ Â*++++
using System;
using System.Reflecti on;
namespace AOP.Experiment
{
class cTestManager
{
[STAThread]
static void Main(string[] args)
{
Assembly TestAssembly;
cTestClass TestClass;
try
{
Console.WriteLi ne("dd");
TestAssembly =
Assembly.LoadFr om("E:\\DR\\Mod ule\\Debug\\Tes tClass.dll");

Console.WriteLi ne(TestAssembly .GetName().ToSt ring());
TestClass =
(cTestClass)Tes tAssembly.Creat eInstance("AOP. Experiment.cTes tClass");
Console.WriteLi ne(TestClass.To String());
}
catch(Exception e)
{
Console.WriteLi ne(e.ToString() );
}
Console.ReadLin e();
}
}

-----------------------------------------------------------------------------------

The result what I supposed is , after I run cTestManager.Ma in(),
Preprocess() in TestAttribute.c s should run before

TestAssembly.Cr eateInstance("A OP.Experiment.c TestClass");

But the result was not what I expected, Preprocess() wasn't executed. If I
apply this attribute on Class scope, Preprocess() would executed. I don't
know why.

Nov 17 '05 #1
0 1615

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1474
by: Nick | last post by:
My client uses a SQL Database to store their usernames and passwords, and I do not believe they have AD...no big deal... I wrote a class to create a generic identity and generic principal so that I can use the .IsInRole function for some added security. I would like to do the same by applying an attribute to a method or class. The code I am including works from what I can see, but I am experiencing the following... 1) I cannot add the...
8
5038
by: Martin Lapierre | last post by:
I try to make a custom CodeAccessSecurityAttribute, but hit the wall at every corner. I created a simple custom security attribute, which is working (see below). But... 1) I have to put the assembly in "C:\Program Files\Microsoft Visual Studio ..NET 2003\Common7\IDE": shoudn't it be the application's directory instead? 2) I'm unable to debug it. Any clues as how to change the required directory and how to debug the
2
2384
by: Zach Mortensen | last post by:
I can't seem to get dynamically-compiled JScript code to use C#-defined custom attributes. I have a simple attribute and a class defined in a C# assembly: namespace MyNamespace { public abstract class MyCsharpAttribute : Attribute { }
0
1148
by: Andrew Venmore | last post by:
Hello, I am having a problem adding an assembly to the toolbox where it appears VS2003 is unable to make sense of a type I am trying to initialise an attribute with. Below is the simplest example which demonstrates the problem. I have two class library projects in my solution: BaseLib and ControlLib. BaseLib declares a custom attribute and an enum, and ControlLib references BaseLib and has a toolbox class and uses the custom attribute.
2
3534
by: Harry F. Harrison | last post by:
I get 2 compile errors on assembly attributes after creating a custom attribute. If I comment out the attribute, the errors go away. I don't get it because my attribute specifies class usage, not assembly usage. Assembly attribute 'System.Runtime.InteropServices.GuidAttribute' is not valid: Assembly custom attribute 'System.Runtime.InteropServices.GuidAttribute' was specified multiple times with different values Attribute...
4
2238
by: techsupport | last post by:
I have some experience with .NET Remoting, as well as ASP.NET 2.0, and have been wanting to remote a custom membership and profile provider. I want to take advantage of the new controls in ASP.NET 2.0 such as Login, Loginuser, Loginview, etc. The ASP.NET provider model requires entries in the web.config for a 'connectionStringName', which I understand is utilized to connect to the data source. Problem is the client machine (machine A)...
2
2529
by: prabhupr | last post by:
Hi Folks I was reading this article (http://www.dotnetbips.com/articles/displayarticle.aspx?id=32) on "Custom Attribute", written by Bipin. The only thing I did not understand in this article is the usage of "Custom Attribute" in real life project. Can somebody please help me understand where are these informations really helpful in Development Environment; may be a few example(s) will help me understand.
0
1286
by: =?Utf-8?B?R3JlZw==?= | last post by:
How can I create a custom assembly attribute for a class and have it displayed as an item in the Version Tab in the DLL Properties dialog box when you right mouse click on the DLL file itself? Here is my first attempt: ' Custom assembly attribute. <AttributeUsage(AttributeTargets.Assembly, Inherited:=False, AllowMultiple:=False)_ Public Class MyAttribute Inherits Attribute
2
5113
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application = Windows Forms class B = a singleton hosted within A. B is responsible for dynamically loading classes X, Y, and Z.
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9544
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.