Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press
Buy directly from Paradoxal Press at $33.99 (Save 43%)
Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA
|
Listing 22-33 extracted from chapter
.NET Remoting
Listing 22-32< > Listing 22-34
This listing can be compiled with the command line: csc.exe /target:exe Example_22_33.cs Errors: 0 Warnings: 0
Example_22_33.cs
using System;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Activation;
using System.Threading;
public class LogContextProperty : IContextProperty {
public LogContextProperty(string sFileName) { m_sFileName = sFileName; }
string m_sFileName;
public string sFileName { get { return m_sFileName; } }
public string Name { get { return "Log"; } }
public bool IsNewContextOK( Context ctx ) { return true; }
public void Freeze( Context ctx ) { }
public void Log( string sLog ) {
// We just write logs on the console.
Console.WriteLine( "ContextID={0} To write '{1}' in the file '{2}'",
Thread.CurrentContext.ContextID,
sLog,
m_sFileName);
}
}
[AttributeUsage(AttributeTargets.Class)]
public class LogContextAttribute : Attribute, IContextAttribute {
string m_sFileName;
public LogContextAttribute(string sFileName){ m_sFileName = sFileName; }
// No need to create a new context if the current one already
// contains the proper context property.
public bool IsContextOK( Context currentCtx,
IConstructionCallMessage ctor) {
LogContextProperty prop = currentCtx.GetProperty( "Log" )
as LogContextProperty;
if ( prop == null ) return false;
return ( prop.sFileName == m_sFileName );
}
public void GetPropertiesForNewContext(IConstructionCallMessage ctor) {
IContextProperty prop = new LogContextProperty( m_sFileName );
ctor.ContextProperties.Add( prop );
}
}
[LogContextAttribute("LogFoo.txt")]
public class Foo : ContextBoundObject {
public Foo CreateNewInst() { return new Foo(); }
public int Add( int a, int b ) {
string s = string.Format("Add {0}+{1}", a, b);
Context ctx = Thread.CurrentContext;
LogContextProperty logger = ctx.GetProperty("Log")
as LogContextProperty;
logger.Log( s );
return a + b;
}
}
public class Program {
static void Main() {
Foo obj1 = new Foo();
obj1.Add(4, 5);
Foo obj2 = new Foo();
obj2.Add(6, 7);
Foo obj3 = obj1.CreateNewInst();
obj3.Add(8, 9);
}
}
Copyright Patrick Smacchia 2006 2007
|