Passing values back and forth appdomains - c#

I have the following code:
public class AppDomainArgs : MarshalByRefObject {
public string myString;
}
static AppDomainArgs ada = new AppDomainArgs() { myString = "abc" };
static void Main(string[] args) {
AppDomain domain = AppDomain.CreateDomain("Domain666");
domain.DoCallBack(MyNewAppDomainMethod);
Console.WriteLine(ada.myString);
Console.ReadKey();
AppDomain.Unload(domain);
}
static void MyNewAppDomainMethod() {
ada.myString = "working!";
}
I thought make this would make my ada.myString have "working!" on the main appdomain, but it doesn't. I thought that by inhering from MarshalByRefObject any changes made on the 2nd appdomain would reflect also in the original one(I thought this would be just a proxy to the real object on the main appdomain!)?
Thanks

The problem in your code is that you never actually pass the object over the boundary; thus you have two ada instances, one in each app-domain (the static field initializer runs on both app-domains). You will need to pass the instance over the boundary for the MarshalByRefObject magic to kick in.
For example:
using System;
class MyBoundaryObject : MarshalByRefObject {
public void SomeMethod(AppDomainArgs ada) {
Console.WriteLine(AppDomain.CurrentDomain.FriendlyName + "; executing");
ada.myString = "working!";
}
}
public class AppDomainArgs : MarshalByRefObject {
public string myString { get; set; }
}
static class Program {
static void Main() {
AppDomain domain = AppDomain.CreateDomain("Domain666");
MyBoundaryObject boundary = (MyBoundaryObject)
domain.CreateInstanceAndUnwrap(
typeof(MyBoundaryObject).Assembly.FullName,
typeof(MyBoundaryObject).FullName);
AppDomainArgs ada = new AppDomainArgs();
ada.myString = "abc";
Console.WriteLine("Before: " + ada.myString);
boundary.SomeMethod(ada);
Console.WriteLine("After: " + ada.myString);
Console.ReadKey();
AppDomain.Unload(domain);
}
}

Related

Static predefined constant objects in a class

I am working on a C# game that will have predefined levels. I am trying to have a class that will hold the predefined data of all of the levels. Here's what I'm trying to do:
public static GameLevel startLevel = new Level() {
startLevel.Actions.Add(action);
startLevel.Actions.Add(action);
}
And so on. However, it seems that C# does not want me to initialize this way. How can I achieve my desired effect without throwing it into a massive constructor?
How do you think if we change the static variable as below:
private static GameLevel _startLevel;
public static GameLevel StartLevel
{
get
{
if(_startLevel == null)
{
_startLevel = new Level();
_startLevel.Action.Add(action1);
_startLevel.Action.Add(action2);
}
return _startLevel;
}
}
Since you have predefined levels, I suggest a little different approach.
Create a Level base class, and a class for each Level. The constructor for each level class can set up the Actions and any other things the game needs to know how to display itself.
using System;
public class Program
{
public static void Main()
{
new GameState(new Level1());
Console.WriteLine("Current level is " + GameState.CurrentLevel.Name);
Console.WriteLine("User leveled up");
GameState.CurrentLevel = new Level2();
Console.WriteLine("Current level is " + GameState.CurrentLevel.Name);
}
}
public class Level
{
public string Name;
// public static IEnumerable<Action> Actions { get; set; }
}
public class Level1 : Level
{
public Level1()
{
// level 1 init
Name = "1";
// Actions = new List<Action> { ... }
}
}
public class Level2 : Level
{
public Level2()
{
// level 2 init
Name = "2";
}
}
public class GameState
{
public static Level CurrentLevel { get; set; }
public GameState(Level startLevel)
{
CurrentLevel = startLevel;
}
}
Working copy: https://dotnetfiddle.net/qMxUbw
"...C# does not want me to initialize this way..."
You can init this way. You simply don't have the right syntax. This should work
public static Level startLevel = new Level()
{
Actions = new List<Action>()
{
new Action() {...},
new Action() {...}
},
OtherProprty = "Other"
};
NOTE: this has to be done under class scope
"Massive constructor" - you usually don't init static members in constructor unless this is static constructor. Sounds like you need to use Singleton pattern for this piece. Then again, you call all the needed code in constructor, "massive" or not. Break it into methods.

Program does not contain a static 'main' method suitable for an entry point : i have tries even removing access modifier before static

using System;
namespace inheritance1
{
public class Employee
{
public string FirstName;
public string LastName;
public string Email;
public void PrintFullName()
{
Console.WriteLine(FirstName + " " + LastName);
}
}
public class FullTimeEmployee : Employee
{
public float YearlySalary;
}
public class PartTimeEmployee : Employee
{
public float HourlyRate;
}
public class Program
{
public static void main(String[] args)
{
FullTimeEmployee FTE = new FullTimeEmployee();
FTE.FirstName = "Max";
FTE.LastName = "Striker";
FTE.YearlySalary = 500000;
FTE.PrintFullName();
PartTimeEmployee PTE = new PartTimeEmployee();
PTE.FirstName = "king";
PTE.LastName = "Maker";
PTE.HourlyRate = 500;
PTE.PrintFullName();
}
}
}
It is Main with an upper case M, not main.
public static void Main(String[] args)
{
Main () and Other Methods (C# vs Java)
Every C# application must contain a single Main method specifying
where program execution is to begin. In C#, Main is capitalized,
while Java uses lowercase main.

Access static object from other instance

I have a Program class which has:
private static ClientBase objClientBase = new ClientBase(new List<RecordType> { RecordType.none }, ModuleType.Monitor);
static void Main(string[] args)
{
objClientBase.Connect(); //IRRELEVANT
objQueueMon = new Main(); //<-INSIDE THIS IS WHERE I WANT TO ACCESS objClientBase
objClientBase.MainModuleThreadManualResetEvent.WaitOne(); //IRRELEVANT
}
This Progam creates a Main class instance as you see:
objQueueMon = new Main();
Notice that they are separated in different files, but the Main class instance is created inside the Program class.
Inside my Program class I want to access that objClientBase.
Do I have to create a constructor method and pass it or make a public access to it?
So what I want to achieve is, inside the Main class, do a objClientBase.FUNCTION
You can do exactly what you just said:
public class Main {
private ClientBase _caller;
public Main (ClientBase caller) {
_caller = caller;
}
}
Or, you can set it later
public class Main {
private ClientBase _caller;
public Main () {
}
// only your assembly sets it
internal SetClientBase(ClientBase cb) {
_caller = cb;
}
// but anyone gets it
// Now you can let some client execute "Function"
public ClientBase Caller {
{return _caller;}
}
}
Just an example
Change the constructor of your Main class to accept a ClientBase object, like this:
public class Main
{
private ClientBase _clientBase;
public Main(ClientBase clientBase)
{
_clientBase = clientBase;
}
public void SomeMethod()
{
// Use ClientBase.FUNCTION here
_clientBase.FUNCTION();
}
}

c#: Calling common method of stored references to class objects from object[] array

Is using a switch on object types really the main way of calling a common function of stored references to class objects? It doesn't have to be a 'object' value type.
using System;
public class MainClass { public void Main() { print "hello world"; } }
public class SubClassOne : MainClass { }
public class SubClassTwo : MainClass { }
public class Storer
{
public void Main() {
object[] objects = new object[2];
objects[0] = new SubClassOne();
objects[1] = new SubClassTwo();
for(i=0;i<2;i++)
{
switch(objects[i].GetType().ToString())
{
case: "SubClassOne":
SubClassOne subclass = objects[i];
subclass.Main();
break;
case: "SubClassTwo":
SubClassTwo subclass = objects[i];
subclass.Main(); //Could probably call after the switch
break;
}
}
}
}
Note: Code not parsed, so there may be serious errors.
"Stringly" typed object oriented code is such a bad idea. You (almost) never need to know the type of an object via a string.
Changing your "print" to Console.WriteLine and main to this works fine
MainClass[] stuff = new MainClass[2];
stuff[0] = new SubClassOne();
stuff[1] = new SubClassTwo();
foreach(var item in stuff)
{
item.Main();
}
If the problem is you are determined to use an array of object, AlexH has answered.
In that case, I suggest to use as keyword to perform a safe cast operation :
using System;
public class MainClass { public void Main() { print "hello world"; } }
public class SubClassOne : MainClass { }
public class SubClassTwo : MainClass { }
public class Storer
{
public void Main() {
object[] objects = new object[2];
objects[0] = new SubClassOne();
objects[1] = new SubClassTwo();
for(i=0;i<2;i++)
{
var myMainClass = objects[i] as MainClass;
if (myMainClass != null)
{
myMainClass.Main();
}
}
}
}
As wudzik said it should be even better to declare objects as a MainClass array
There are many ways of solving this in a nice way, depends on:
If you know types and there are not too much of them:
Use LINQ OfType<>(). For more details see MSDN
foreach (var item in objects.OfType<SubClassOne>())
{
item.Main();
}
foreach (var item in objects.OfType<SubClassTwo>())
{
item.Main();
}
If there are many types, just introduce common interface
interface ISharedApi
{
void Main();
}
class SubClassOne : ISharedApi
class SubClassTwo : ISharedApi
And implement this/mark each type by it, then you just need single loop:
var objects = new List<ISharedApi>();
objects.Add(new SubClassOne());
objects.Add(new SubClassTwo());
foreach (var item in objects)
{
item.Main();
}
You should implement a more object oriented solution. Instead of creating an array consisting of objects you should make MainClass abstract and define an abstract method Main. After that you should implement Main in you sublclasses.
In this way you can exchange your code to:
using System;
public abstract class MainClass { public abstract void Main(); }
public class SubClassOne : MainClass {
public override void Main() { print "SubClassOne, hello world"; }
}
public class SubClassTwo : MainClass {
public override void Main() { print "SubClassTwo, hello world"; }
}
public class Storer
{
public void Main() {
MainClass[] objects = new MainClass[2];
objects[0] = new SubClassOne();
objects[1] = new SubClassTwo();
foreach(MainClass mc in objects)
{
mc.Main();
}
}
}

C# StreamWriter , write to a file from different class?

How can I write to a file from different class?
public class gen
{
public static string id;
public static string m_graph_file;
}
static void Main(string[] args)
{
gen.id = args[1];
gen.m_graph_file = #"msgrate_graph_" + gen.id + ".txt";
StreamWriter mgraph = new StreamWriter(gen.m_graph_file);
process();
}
public static void process()
{
<I need to write to mgraph here>
}
Pass the StreamWriter mgraph to your process() method
static void Main(string[] args)
{
// The id and m_graph_file fields are static.
// No need to instantiate an object
gen.id = args[1];
gen.m_graph_file = #"msgrate_graph_" + gen.id + ".txt";
StreamWriter mgraph = new StreamWriter(gen.m_graph_file);
process(mgraph);
}
public static void process(StreamWriter sw)
{
// use sw
}
However your code has some, difficult to understand, points:
You declare the class gen with two static vars. These vars are
shared between all instances of gen. If this is a desidered
objective, then no problem, but I am a bit puzzled.
You open the StreamWriter in your main method. This is not really
necessary given the static m_grph_file and complicates the cleanup in case your code raises
exceptions.
For example, in you gen class, (or in another class) you could write methods that work on the same file because the file name is static in the class gen
public static void process2()
{
using(StreamWriter sw = new StreamWriter(gen.m_graph_file))
{
// write your data .....
// flush
// no need to close/dispose inside a using statement.
}
}
You can pass a StreamWriter object as a parameter. Alternatively you could create a new instance inside your process method. I would also recommend wrapping your StreamWriter inside a using:
public static void process(StreamWriter swObj)
{
using (swObj)) {
// Your statements
}
}
Of course you could simply have your 'process' method like this:
public static void process()
{
// possible because of public class with static public members
using(StreamWriter mgraph = new StreamWriter(gen.m_graph_file))
{
// do your processing...
}
}
But from the design point of view this would make more sense (EDIT: full code):
public class Gen
{
// you could have private members here and these properties to wrap them
public string Id { get; set; }
public string GraphFile { get; set; }
}
public static void process(Gen gen)
{
// possible because of public class with static public members
using(StreamWriter mgraph = new StreamWriter(gen.GraphFile))
{
sw.WriteLine(gen.Id);
}
}
static void Main(string[] args)
{
Gen gen = new Gen();
gen.Id = args[1];
gen.GraphFile = #"msgrate_graph_" + gen.Id + ".txt";
process(gen);
}

Categories

Resources