Consider following piece of code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RemotingNonVirtualCall
{
class Program
{
static void Main(string[] args)
{
var domain = AppDomain.CreateDomain("Second Domain");
A extA = (A)domain.CreateInstanceAndUnwrap(typeof(A).Assembly.FullName, typeof(A).FullName);
Console.WriteLine(extA.CurrentDomain());
}
}
[Serializable]
sealed class A : MarshalByRefObject
{
public string CurrentDomain()
{
return AppDomain.CurrentDomain.FriendlyName;
}
}
}
Method A::CurrentDomain is non-virtual, class A is sealed. But CLR intercepts method call and redirect it to another instance. How it is possible? Is it some sort of voodoo magic? Does CLR make some exception in method calling for object inherited from MarshalByRefObject class? How is it performed?
Thanks for advance.
It's essentially magic, i.e. the ability to do this is built into the .NET runtime. The good news is that your code can also do this, if it needs to: http://msdn.microsoft.com/en-us/library/system.runtime.remoting.proxies.realproxy.aspx
The JIT compiler is keenly aware that it generates code for a proxy. You can have a look-see with the SSCLI20 source code, clr\src\vm\jithelpers.cpp, search for "proxy".
Related
In Nunit C# Im trying to open the application only once and make modification to it, for example i created this demo of Notepad application. In my real time project,i just have to log in to the application just once and execute all 100 test cases and only then close the Desktop application. Kindly tell me what am i doing wrong here,thanks a lot! Btw,im new to C#
using NUnit.Framework;
using OpenQA.Selenium.Remote;
using System;
using OpenQA.Selenium;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Notepad
{ }
[SetUpFixture]
public class BaseClass
{
public static IWebDriver driver;
[OneTimeSetUp]
public static void AssemblyInitalize()
{
var dc = new DesiredCapabilities();
dc.SetCapability("app", #"C:\\Windows\\System32\\notepad.exe");
driver = new RemoteWebDriver(new Uri("http://localhost:9999"), dc);
Thread.Sleep(5000);
}
[OneTimeTearDown]
public static void oneTearDown()
{
driver.FindElement(By.Id("Close")).Click();
}
}
---First Test---
namespace Notepad
{ [TestFixture]
public class Notepad2:BaseClass
{
[Test]
public void test2()
{
driver.FindElement(By.Id("15")).SendKeys("My Teacher ");
}
}
}
---- Second Test Class ----
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
namespace Notepad
{ [TestFixture]
public class NoteTest : BaseClass
{
[Test]
public void Test()
{
driver.FindElement(By.Id("15")).SendKeys("...is Coming now");
}
}
}
Regarding SetUpFixture, the first answer is incorrect.
SetUpFixture works exactly the same way as it has always worked. If you have a SetUpFixture, it's OneTimeSetUp and OneTimeTearDown methods will run once for all the fixtures under the same namespace.
On the other hand, inside a TestFixture the OneTimeSetUp and OneTimeTearDown methods run just once for the fixture itself.
In other words, the methods run "one time" within the particular scope in which they are used, namespace or fixture.
The problem is that you are using the same class as both a SetUpFixture and as the base class for all your TestFixtures. That means if you have n test fixtures, it will run n + 1 times! That's what you are seeing.
SetUpFixtures do not (have never had) anything to do with fixture inheritance. You should either make your class a SetUpFixture or a TestFixture base class. If, for whatever reason, you need both, then use two classes. In this case, you need only the SetUpFixture, without the inheritance.
When to inherit from a base class: When you want the same code to execute many times, once for each fixture. Note that your base class, when used for one-time setup and teardown in this way, should normally not be marked as a TestFixture.
When to use a SetUpFixture: When you want some code to execute only once, before every any fixture runs and again once after all the fixtures have run.
In your example, you are using a SetUpFixture to control the timing of the initialization. You are using inheritance to allow you to share the driver. Problem is that the OneTimeSetUp in the base class is actually part of each test fixture and you don't want that.
I'd be concerned about a hundred or so tests all using the same driver. I've seen some people use one driver per test and others one driver per fixture_. Using one for __everything implies that you are being extraordinarily careful that each test cleans up after itself and restores the driver to the same state. I doubt that's possible.
However, as an exercise, here is how to do it if you really want to:
1. Have a base class with nothing but the driver member.
2. Derive the SetUpFixture from the base class and create / destroy the driver there.
3. Derive the TestFixtures from the same base class. They use but don't change the driver.
From the documentation, OneTimeSetup is called once before all tests in a TestFixture. Your example has two fixtures, so the set up is called twice. You need to have all your tests in the same Fixture.
This behaviour is different to the old [Setup] on SetupFixture attribute, which ran once for all tests in a namespace.
All, I'm new to C# and I'm trying to work through a problem that I'm having a hard time understanding.
I'm referencing a library in my code (which I've successfully added to my references). I've also identified the specific namespace within the library to bring in. However, when I initialize an instance of a class within said namespace I'm running into some issues.
The class in question has a constructor (I believe) that has an IntPtr argument. When I'm initializing an instance of the class in my code it's expecting me to provide it with an IntPtr argument, but I don't understand what exactly I should be providing. I know that IntPtr is a pointer to a space in memory, but what that means in my code I'm not sure.
Here is a link to the Library in question: https://github.com/filoe/cscore
The Namespace/Class in reference is in my code below.
using System;
using CSCore.CoreAudioAPI;
namespace EndpointController_POC_v0._1
{
class Program
{
static void Main(string[] args)
{
AudioEndpointVolume endPtCtrl = new AudioEndpointVolume(?);
endPtCtrl.SetMasterVolumeLevelScalar(1, Guid.Empty);
Console.ReadLine();
}
}
}
I m reading a Kudan for Unity exaple project
(Kudan is a framework for AR)
in the file KudanTracker.cs they use a TrackerBase object called _trackerPlugin
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Kudan.AR
{
[DisallowMultipleComponent]
[RequireComponent(typeof(Camera))]
[AddComponentMenu("Kudan AR/Kudan Tracker")]
public class KudanTracker : MonoBehaviour
{
protected TrackerBase _trackerPlugin;
public bool ArbiTrackIsTracking()
{
return _trackerPlugin.ArbiTrackIsTracking();
}
...
}
So i head over to TrackerBase.cs to see the implementation for ArbiTrackIsTracking() but all i find is this:
using UnityEngine;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Kudan.AR
{
/// <summary>
/// Base class for the tracker plugin. This abstracts the native plugin for each operating system.
/// </summary>
public abstract class TrackerBase : ITracker
{
public abstract bool ArbiTrackIsTracking ();
...
}
}
How is it possible to call a method that was never implemented?
where is the implementation hiding?
thank you
They probably deal with it, like Microsoft dealt with the XmlReader:
You are using a derived class for the object you are using, but access it via its base class.
In the XmlReader it goes like this:
XmlReader reader = XmlReader.Create("something");
and then you can read every element within the xml-file via
reader.Read();
But as you can see, XmlReader.Read() isn't even implemented: Source
Instead, XmlReader.Create() creates an XmlTextReader which inherits from XmlReader and implements everything, especially how Read() is handled.
I guess they do the same in the plugin. See here in the API it states "Implemented in Tracker" KudanAR - Unity Plugin V1.4
This is perfectly normal.
Such behavior is normal, IF the class is properly instantiated with a non-abstract version of Trackerbase.
However, you don't provide the relevant instantiation code, so its impossible for me to verify if the code sample you provided is properly instantiated or not, without myself going to get that code.
Theoretically, the instantiation code might look like this:
//Constructor
public KudanTracker(TrackerImplementation track) {
_trackerPlugin = track;
}
IF the code has proper instantiation, such as above, you're fine.
It's impossible to call an abstract method without any implementation.
As the comment for TrackerBase class says, Tracker classes are different for each OS, so look for TrackerWin.cs or something like that
I have this code to create a simple .NET .dll. It only returns an int.
But, it is not working inside Java.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ReturnINT
{
public class ReturnINT
{
public static int RetornaInteiro ()
{
try
{
int number = 2;
return number;
}
catch (Exception)
{
return 1;
}
}
}
}
How can I call the method from within Java?
When I Use JNI i have this error IN java:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Dll.RetornaInteiro()V
at Dll.RetornaInteiro(Native Method)
at Dll.main(Dll.java:27)
Check the http://www.javonet.com as well. With one-jar file you can load this dll and call as follows:
Javonet.AddReference("your-lib.dll");
int result = Javonet.getType("ReturnINT").Invoke("RetornaInteiro");
Javonet will automatically load your library in .NET process and give you access to any classes and types contain within it. Next you can get your type and invoke static method. Method results and arguments are automatically translated between JAVA and .NET types. You can pass for example string or bool arguments like that
Boolean arg1 = true;
String arg2 = "test";
Javonet.getType("ReturnINT").Invoke("MethodWithArguments",arg1,arg2);
And they will be translated automatically.
In addition you can also create instance of your type, subscribe events, set/get properties and fields, handle exceptions or even pass value-type arguments. Check the docs for more details:
http://www.javonet.com/quick-start-guide/
PS: I am member of Javonet team. Therefore feel free to ask me any detailed questions regarding native integrations and our product.
You can call it directly: http://jni4net.sourceforge.net/
Or you can call it as an executable.
It looks cool on MSDN:
Specifies that the method is declared, but its implementation is
provided elsewhere.
So I tried it in a console application:
public class Program
{
[MethodImplAttribute(MethodImplOptions.ForwardRef)]
public static extern void Invoke();
static void Main(string[] args)
{
Invoke();
Console.Read();
}
}
Then what should I do now? Where can I provide the implementation of Program.Invoke?
The usage of ForwardRef goes pretty much like this:
consumer.cs
using System;
using System.Runtime.CompilerServices;
class Foo
{
[MethodImplAttribute(MethodImplOptions.ForwardRef)]
static extern void Frob();
static void Main()
{
Frob();
}
}
provider.cs
using System;
using System.Runtime.CompilerServices;
class Foo
{
// Need to declare extern constructor because C# would inject one and break things.
[MethodImplAttribute(MethodImplOptions.ForwardRef)]
public extern Foo();
[MethodImplAttribute(MethodImplOptions.ForwardRef)]
static extern void Main();
static void Frob()
{
Console.WriteLine("Hello!");
}
}
Now the magic sauce. Open a Visual Studio command prompt and type:
csc /target:module provider.cs
csc /target:module consumer.cs
link provider.netmodule consumer.netmodule /entry:Foo.Main /subsystem:console /ltcg
This uses one of the lesser known functionality of the linker where we're linking managed modules together. The linker is able to gel together same-shaped types (they need to have the exact same methods, etc.). ForwardRef is the thing that actually lets you provide implementation elsewhere.
This example is kind of pointless, but you can imagine things getting more interesting if a single method is implemented in a different language (e.g. IL).
My understanding is that ForwardRef acts in the same way as extern, and is intended for guiding the runtime when the language you are using lacks direct support (via extern in C#). As such, the usage should be very similar to the extern modifier, most notably using [DllImport(...)].