ToString() with Parameter and Parse/TryParse with parameters

Leave a comment

When using object.ToString(), or <primitive>.Parse(), TryParse(), any more ideas?

Ya… Es ist…

  1. Which language and Culture are you handling with?
  2. Do you want to format via IFormattable interface or parsing it?

MSDN Link :

  •   Help View Home/Visual Studio 2010/.Net Framework 4/.Net Framework Core Development/Working with Base Type/Formatting Types or Parsing Strings [strongly recommend]
  •   CLR via C# Version 3, Jeffery Richter – Page 309

Just shared with you the code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Globalization;

namespace _14_Strings
{
class Program
{
/// <summary>
/// Static method for formatting of Deutsch string -> Help View Home/Visual Studio 2010/.Net Framework 4/.Net Framework Core Development/Working with Base Type/Formatting Types or Parsing Strings
/// Jeffery Richter – Page 309
/// </summary>
static void IFormatProviderTest()
{
//foreach (CultureInfo item in System.Globalization.CultureInfo.GetCultures(CultureTypes.AllCultures)) //CultureTypes Enumeration
//{
//    Trace.WriteLine(item.ToString());
//}
decimal lNumericBase = 10000000L;//Int64->long
Trace.WriteLine(lNumericBase.ToString(“C”, new CultureInfo(“de-DE”)));
//Trace.WriteLine(lNumericBase.ToString(“D”, new CultureInfo(“de-DE”))); //Invalid format
Trace.WriteLine(lNumericBase.ToString(“N”, new CultureInfo(“de-DE”)));
string strNumericBase = lNumericBase.ToString(“N”, new CultureInfo(“de-DE”));

long number;
IFormatProvider provider = CultureInfo.CreateSpecificCulture(“de-DE”);

if (long.TryParse(strNumericBase, NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint, //NumberStyles.Number -> Group NumberStyles Enumeration
provider, out number))
{
Trace.WriteLine(number);
}

strNumericBase = lNumericBase.ToString(“N”, new CultureInfo(“de-DE”));
if (long.TryParse(strNumericBase, NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint, //NumberStyles.Number -> Group NumberStyles Enumeration
provider, out number))
{
Trace.WriteLine(number);
}
}

static void Main(string[] args)
{
Program.IFormatProviderTest();
}
}
}

Comparison in Run-time:


Advertisements

C# STA Thread “Message Loop”

Leave a comment

seems a little bit impression, what’s the hell of Thread Message Loop.

  • Use Case – we have a STA DLL, which is exposed for us several COM/COM+ methods. And internally such DLL will store internal “status” cache, for instance STA instance, SA&G.
  • Key Points
  1. Explicit STA Thread construction – Thread.setApartment(STA/MTA). MTA is default setting.
  2. Supposed we call start(), and stop() for default instantiation logic of DLL, how about additional method called inside thread? Can we? Yes, just do something like Message loop. When performance has been concerned, EventWaitHandle (Mutex, Sep. etc)….

public class MethodProxyInThread
{
public MethodProxyInThread _realProxy;

private bool _isRealProxyStop = false;

private event EventHandler<EventArgs> _eventHandler;

private ThreadLocal<EventHandler<EventArgs>> _handler;

private Func<object, int> _functionalHook;

private EventWaitHandle waitHandle;

private object parameter;

/// <summary>
/// in one thread context
/// </summary>
/// <param name=”_realProxy”></param>
public void startMethod(object _realProxy)
{
if (this._realProxy == null)
{
this._realProxy = _realProxy as MethodProxyInThread;
this._isRealProxyStop = false;
Console.WriteLine(string.Format(“startMethod – {0}”, Thread.CurrentThread.ManagedThreadId));
Console.WriteLine(“startMethod”);

waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
this._handler = new ThreadLocal<EventHandler<EventArgs>>(() => new EventHandler<EventArgs>(this.MethodProxyInThread__eventHandler));
this._eventHandler += this._handler.Value;

//TODO : Call start Method Here
//startCall() here
do
{
Console.WriteLine(string.Format(“loopMethod – {0}”, Thread.CurrentThread.ManagedThreadId));
if (this.waitHandle.WaitOne(5000))//rest if no more action, what time-out? to avoid deadlock!
{
//TODO : invoke conditional method; -> parameters
this._functionalHook.Invoke(this.parameter);//, null, null); //=>asynchorous logic
if (!this._isRealProxyStop)
{
this.waitHandle.Reset();
}
}
} while (!this._isRealProxyStop);
//stopCall() here
this._eventHandler -= this._handler.Value;
this.waitHandle.Close();
Console.WriteLine(“Quit the thread”);
}
}

private void MethodProxyInThread__eventHandler(object obj, EventArgs e) //Guys, why this isn’t working, delegation will be running in ThreadPool
{
Console.WriteLine(string.Format(“MethodProxyInThread__eventHandler – {0}”, Thread.CurrentThread.ManagedThreadId));
this._isRealProxyStop = true;
}

//public void PostTerminalEvent()
//{
//    if (MethodProxyInThread._eventHandler != null)
//    {
//        Console.WriteLine(string.Format(“PostTerminalEvent – {0}”, Thread.CurrentThread.ManagedThreadId));
//        MethodProxyInThread._eventHandler(this, new EventArgs());
//    }
//}

public void PostMessage(Func<object, int> _func, object _param)
{
if (!this._isRealProxyStop)
{
this._functionalHook = _func;
this.parameter = _param;

this.waitHandle.Set();
}
}

public void stopMethod()
{
Console.WriteLine(string.Format(“stopMethod – {0}”, Thread.CurrentThread.ManagedThreadId));
this._isRealProxyStop = true;
}
}

class Program
{
static int PostMessageObj(object _obj)
{
Console.WriteLine(string.Format(“postMethod – {0}”, Thread.CurrentThread.ManagedThreadId));
return 5;
}

static void Main(string[] args)
{
MethodProxyInThread proxy = new MethodProxyInThread();
Thread t = new Thread(proxy.startMethod);
t.Start(proxy);

Thread.Sleep(5000);
proxy.PostMessage(new Func<object, int>(Program.PostMessageObj), 6);
Thread.Sleep(5000);
proxy.stopMethod();
//proxy.PostTerminalEvent();
Thread.Sleep(10000);
}
}

 

Of Course, that’s not the ultimate version, For Message Loop, we need dictionary<enum, Func<object, object>> for checking-up, and parameter lists.

Like it, or some more suggestions… Whatever :-). No-UI SynchronizationContext Solution.

AppDomain Remote Proxy Model

Leave a comment

http://www.codeproject.com/KB/IP/CrossDomainRemoting.aspx?display=Print

Loading strategy and reflection for .Net Assemblies

Leave a comment

This blog is to clarify How-to work with reflection to load .Net Assemblies, to avoid some mistakes during programming.

  • The shared assemblies and strong  assemblies

Shared assemblies is a single assembly, which could only be used for private deployment (Saying, deploy to application base folder or its sub folder).

Strong assemblies is a kind of assemblies with signature generated by public/private key, which could be used for private deployment and also global deployment (Saying, deploy to well-known location, such as GAC, internet location, etc).

Both Shared assemblies and Strong assemblies had the same PE(portable exectuable) file format – PE32+ header, CLR header, MetaData header, Manifest and IL(all of .net compatible language will be converted into IL command, like MACRO).

And Strong assemblies could be generated and configured via SN.exe (.Net public&private key generator)toolkit, together with CSC.exe(C# assemblies compiler).

The following step shown for us, how the strong assemblies has been generate:

SN -k MYCompany.snk

SN -tp MyCompany.snk [display public key]

csc /keyfile:MyCompany.snk app.cs [compile into assembly]

  • CSC compiler will generate assemblies with references information [it depends on who, for instance, UIDesigner.exe depends on sapLSConnector.dll]

Take UI Designer assemblies as example, we can use ilasm.exe to get the following sections:

35(0x23): AssemblyRef          cRecs:   25(0x19), cbRec: 24(0x18), cbTable:   600(0x258) -> will be converted into depended assembly.
col  0:  MajorVersion oCol: 0, cbCol:2, USHORT
col  1:  MinorVersion oCol: 2, cbCol:2, USHORT
col  2:  BuildNumber  oCol: 4, cbCol:2, USHORT
col  3:  RevisionNumber oCol: 6, cbCol:2, USHORT
col  4:  Flags        oCol: 8, cbCol:4, ULONG
col  5:  PublicKeyOrToken oCol: c, cbCol:2, blob
col  6:  Name         oCol: e, cbCol:4, string
col  7:  Locale       oCol:12, cbCol:4, string
col  8:  HashValue    oCol:16, cbCol:2, blob

  • OK, back to our topic How-to use API for reflection to load assemblies and their differences.

For Backgournd mechnaism:

First of all, JIT(just in time compiler) will load assemblies into memory and execute IL command(convert IL command into native code).

Take care: JIT compiler will use AssemblyRef and TypeRef metaData to fetch  Assembly Name(without extsion name and path), Version, language, and public key token.

So if you use reflection API to load assemblies,

You will always works with identification token – Assembly Name(without extension name and path), Version, language, and public key token. And for shared assemblies , identification token will be just Assembly Name(without extsion name and path).

Yeah, welcome, the background story stopped, let’s go to API analysis.

For class Assembly, we have the following versions to load assemblies, what’s the difference:

  • Group 1 – Assembly.Load()

public static Assembly Assembly.Load(AssemblyName)

public static Assembly Assembly.Load(String)

1, How-to use API

AssemblyName is parameter without extension name and path : for loading UIDesigner.exe,

Code Snappit : Assembly _uidesigner = Assembly.Load(“UIDesigner”);

//in ILasm.exe, you will see UIDesigner.exe have Assembly name “UIDesigner”, please do check before you wrote code, as assembly name mayn’t be the same as dll name.

Or using string for assemblystring [with full information],

Code Snappit : Assembly _uidesigner = Assembly.Load(“UIDesigner, Version=25.0.555.1045, Culture=neutral, PublicKeyToken=null”);

Clearly see here that: UI Designer won’t own one public/private key pair.

2, Important thing – The searching path

  1. internally Load method will use Version rebinding strategy to load assembly in GAC (Global assembly cache), if not, go to 2;
  2. it will go to application base directory, private path subfolder and codebase position(you can define codebase in app.config file), if not go to3.
  3. throw out exception – FileNotFoundException.

Three points here:

  1. Such searching strategy keeps the same as Win32 LoadLibrary;
  2. for shared assemblies[some one called “weak assemblies”], .Net runtime won’t got for Version rebinding strategy AND GAC. directly go to step2.
  3. you can use AppDomain.appendPrivatePath() API or section of probing in app.config to configure private path subfolder.
  • UI Designer’s issue exists here:

In our PDI AppDomain, when creating UI Designer AppDomain, the application base path is \\….\CopernicusIsolatedShell.exe’s root folder. And when loading UI Designer AppDomain, I have already use AppDomain.appendPrivatePath()  to add “\Extension\Application” for UI Designer AppDomain.

So in UI Designer AppDomain, the API -> Assembly _uidesigner = Assembly.Load(“sapLSUICheckmateCoreWPF”); to load sapLSUICheckmateCoreWPF assemblies [instead of  Assembly _uidesigner = Assembly.Load(“sapLSUICheckmateCoreWPF.dll”) ].  And JIT search in Private Path folder, just found one assembly with Assembly name sapLSUICheckmateCoreWPF. [JIT just search AssemlyRef Sections for all assemblies under search path].

The method is similar to linux library searching strategy, add slib explictly into application search path.

The key points is that before load target assembly, you need to add your customized PrivatePath and after that please let it recovery to avoid additional performance impact. And one more question is that for Microsoft AppDomain design perspective, AppDomain.appendPrivatePath() has already been depercated, and alternative of AppDomainSetup is suggested to use during startup of new AppDomain.

And when you try to load assembly, Assembly.Load() will be always your first choice.

  • Group 2 – Assembly.LoadFrom

public static Assembly Assembly.LoadFrom(String)

public static Assembly Assembly.UnsafeLoadFrom(String)

1, For LoadFrom, you will also try to use FullName[including extension name] of assembly.

take sapLSUICheckmateCoreWPF as example, the correct usage will be

Code Snappit :  Assembly _currentAssembly = Assembly.LoadFrom(@”G:\project_summary\leanstack0_PDI_dev6_FP35_Dev2_Mashupconcept2_backendAPI\OberonLocalUI1_ConsoleAppDomain\bin\Debug\SubFolder\UIDesigner.exe”)

And it allow use URL as parameter:

Code Snappit :  Assembly _currentAssembly = Assembly.LoadFrom(@”http://orlando.com/UIDesigner.exe&#8221;)

For this case, you have to allow some internet security setting, but if you’re trying to ignore internet security setting [seems not so good].

you can use

Code Snappit :  Assembly _currentAssembly = Assembly.UnsafeLoadFrom(@”http://orlando.com/UIDesigner.exe&#8221;)

2,Question: How LoadFrom work? what’s relationship between Load()?

Answers: at the beginning, it will call System.Reflection.AssemblyName.GetAssemblyName() to fetch Assembly Name for JIT [just read target file, and search AssemblyRef metaData, return string then close File]. Then it will call Assembly.Load() method to do the same processing as I mentioned before.

  • Group 3

public static Assembly Assembly.ReflectionOnlyLoadFrom(String FilePath)

public static Assembly Assembly.ReflectionOnlyLoad(String)

1,  For searching path and Version checking-up

ReflectionOnlyLoadFrom method will just load assembly on FilePath, and also ignore GAC and other searching path.

ReflectionOnlyLoad method will search the same searching path as Assembly.Load() done, the only difference is that it won’t check up with Version.

2,  These methods relevant with instance, which are instantiated by these two methods, don’t allow to be executed!

Basically, when we get Assembly instance, we can use reflection to get Type instance, and use dynamically invoke to invoke method calling. But this’s not allowed for this two methods. If you want to have a trial, of course, you can :-). You will get exception – InvalidOperationException.

The reason is that Microsoft design this two methods for different CPU architecture, x64, x86, etc. This method is cross-CPU metaData time [Orlando made this work out, :-)]. But JIT needed one specific CPU architecture for runtime convestion [mentioned before, during runtime, IL command will be converted into Native code.]

Two addtional Questions:

  • If CLR couldn’t find depended DLL,  do we have one second choice to point out for them during runtime?

Answers: Yes

In you AppDomain, you can use the following code to let your AppDomain know that I’ll find it for you.

Code snippet:

AppDomain.CurrentDomain.AssemblyResolve += (sender, argvs) =>
{
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(argvs.Name))
{
byte[] assemblyData = new byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
};

  • AppDomain also have Assembly assembly = AppDomain.Load(string), Can I use it?

Answer: Yes, you can, but take care the following situation [Orlando ever be suffering a lot for this]

In principle, AppDomain.Load() is used for “managed-unmanage” [Orlando made the name, that means also it’s managed code, but it call unmanaged CLR internal implementation finally]operation  to insert assembly into target AppDomain.

And if target AppDomain has its own searching path strategy, [in UI Designer appDomain, we already do such things], you need to take care.

In this case, the searching strategy will fit for the caller of AppDomain.Load(), not your target.

caller : AppDomain.Load() -> get Assembly reference from target AppDomain

As Assembly isn’t MarshalbyRefObject type, so this reference will be passed by value to caller AppDomain. But currently, the searching strategy has been replaced with caller AppDomain.

In target AppDomain and caller AppDomain doesn’t have the same searching strategy. My Godness, hope you good luck. 🙂

  • How about Assembly.LoadFile() ?

Assembly.LoadFile() will be always relevant with FullPath of Assembly. For instance:

Code Snippet:

Assembly _currentAssembly = Assembly.LoadFile(@”G:\project_summary\leanstack0_PDI_dev6_FP35_Dev2_Mashupconcept2_backendAPI\OberonLocalUI1_ConsoleAppDomain\bin\Debug\SubFolder\UIDesigner.exe”);