Access internal method from child namespace - c++-cli

In C++/Cli is it possible to access an internal method from a child namespace without reflection?
Example:
//TestClass.h
namespace Test {
public ref class TestClass {
internal:
void InternalMethod();
};
}
//ChildClass.h
namespace Test {
namespace Child {
public ref class TestClass {
public:
void AccessInternalMethod()
{
TestClass c;
c.InternalMethod();
}
};
}
}
Seems like this should be possible. Sorry if this has been asked before.

In C++/CLI internal (as well as C# internal and the CLI equivalent assembly) doesn't have anything to do with namespaces. When you use that modifier on a member, it means it's accessible from inside the same assembly, as the one where it is declared.
That means that if Test.Child.TestClass is in the same assembly as Test.TestClass, it can access InternalMethod(). If it's in another assembly, it can't access it. Namespaces have nothing to do with that.

Related

How to access the public variable in plugin1 from plugin2 using OSGI framework

I'm new to OSGI framework and I'm trying to access the 'Derived' Class variable 'publicVariable' from another class 'Derived2' like "Derived.publicVariable" but publicVariable is always shows null. I really appreciate if someone can help me out with this.
Thanks
Manifest file - Derived2
Require-Bundle:com.xxxxxx.Derived1
Java code
abstract class Base {
protected Vector <String> supportedCommands = new Vector <String> ();
protected abstract void initialiseCommands();
}
class Derived extends Base {
private static Derived derivedPlugin = null;
public Derived()
{
derivedPlugin = this;
}
public static Derived getPlugin()
{
return derivedPlugin;
}
public String publicVariable = null;
protected void initialiseCommands()
{
publicVariable = "someData";
System.out.println("Derived" + publicVariable);
}
}
class Derived2 extends Base {
protected void initialiseCommands()
{
supportedCommands.add(Derived.getPlugin().publicVariable);
System.out.println("IMRSAUtilitiesPlugin" +supportedCommands);
}
Also referred below link, which is a similar issue but i'm not using any static variable, it is just a public variable.
how use Singleton object in different class loader....?
The code in the question will not compile. You are trying to access an instance field (publicVariable in class Derived) in a static way, i.e. Derived.publicVariable.
OSGi does not change the semantics of the Java language, and if you cannot even compile your code then OSGi will certainly not be able to run it.

How to wrap a C++ library

I'm developing an application which is using a library and I would like to wrap this library so that it does not goes to deep into my application code. Thanks to that I could change the library I'm using just by re-implementing my wrapper classes.
Suppose that I have a library LibA. It gives me 2 objects to work with, LibAObj1 and LibAObj2. LibAObj2 has a method using LibAObj1.
Here can be a simple definition of their declaration
class LibAObj1 {};
class LibAObj2
{
void action(LibAObj1 &obj);
};
Now I would like to define an interface that my application can use to wrap those objects in my application code
For instance:
class ItfLibAObj1 {};
class ItfLibAObj2
{
public:
void action(ItfLibAObj1 &obj) = 0;
};
The problem comes whenever I want to implement my interface ItfLibAObj2.
class ImplLibAObj2 : public ItfLibAObj2
{
public:
void action(ItfLibAObj1 &itfObj)
{
<how to get my LibAObj1 from itfObj>?
obj.action(LibAObj1);
}
private:
LibObj2 obj;
}
The question is actually in the pseudo code. How to get my LibAObj1 contained in my ItfLibAObj1 reference? I could add a getter function in LibAObj1 interface to return a void pointer that I would cast but I don't find that elegant in C++.
Is there any kind of design pattern I could use to solve my problem? Or do I just have a design issue?
Note that I'm not wishing to select which library to use at run time.
Thanks a lot for your help.
Kind regards
You problem perfectly explains why in Proxy Pattern, both the Real and Proxy must implement the same interface:
And your code should look like this:
// interfaces
class ItfLibAObj1 {};
class ItfLibAObj2
{
public:
void action(ItfLibAObj1 &obj) = 0;
};
// real
class RealLibAObj1 : public ItfLibAObj1 {};
class RealLibAObj2 : public ItfLibAObj2
{
void action(ItfLibAObj1 &obj)
{
...
}
};
// proxy
class ProxyLibAObj1 : public ItfLibAObj1
{
private:
RealLibAObj1 real;
};
class ProxyLibAObj2 : public ItfLibAObj2
{
private:
RealLibAObj2 real;
void action(ItfLibAObj1 &obj)
{
// do something
real.action(obj); // delegate to the real
// do something
}
};
However, if the whole purpose of your "wrapping" is adding a new layer between your core/real and the outside (client), please consider the Facade Pattern which provides a simpler interface to the client, instead of merely mimic the classes/methods of the core.

Can't reference C++/cx class from xaml? (UWP)

I have a C++ class in my application testclient:
namespace testclient{
namespace models{
ref class myclass sealed{
public:
myclass();
property String^ getstring
{
String^ get()
{
return string;
}
}
private:
String^ string = "test";
}}}
I want to bind a control to the property getstring, and from what little I understand of UWP XAML data binding, I have to include this in the top of the MainPage.xaml: xmlns:data="using:testclient.models Problem is, intellisense is telling me "Undefined namespace. The 'using' URI refers to a namespace called testclient.models that could not be found." What am I doing wrong?
EDIT: I've found the problem goes away when I put the class in Mainpage.Xaml.h, but I'd rather not do this...
Every binding consists of a binding target and a binding source. Typically, the target is a property of a control or other UI element, and the source is a property of a class instance.
If you want to use myclass as datasource to MainPage's UI elements, you need to make sure the instance of the myclass is accessible to MainPage. That's why your first version resulted in error. In order to modify mainPage.Xaml.h as little as possible, you could follow steps below by creating a separate file(I simplified the member of myclass for easy debugging):
1) Create myclass.h:
namespace TestClient{
namespace models{
public ref class myclass sealed
{
private:
int test = 1;
public:
myclass()
{
}
property int gettest
{
int get() { return test; };
}
};
}
}
2) in MainPage.h, add following:
#include "myclass.h"
namespace TestClient
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public ref class MainPage sealed
{
private:
TestClient::models::myclass myTest;
.......
}
.........
}
3) Then you can manipulate myclass data in mainPage.cpp as you want. Codes may be like below:
MainPage::MainPage()
{
InitializeComponent();
int i = this->myTest.gettest;
...........
}
Still I have a question: while so many namespace nested? Also you can find a sample about data binding here just for your reference.

Singleton with StructureMap custom convention in ASP.NET MVC 4

I am having an issue trying to get the singleton lifecycle to work with a custom convention in StructureMap.
Basically I have a custom registry type class that contains a dictionary that I would like to be a singleton so that it is created once at startup of the application.
I created a custom convention that will look at an attribute of a class and determine whether or not the class should be HttpContextScoped or Singleton.
The problem is that when I run the application with the Visual Studio debugger the constructor of the object that should be a singleton gets called every time the web page is loaded instead of happening once as I expected. It looks like the object is behaving as a HttpContextScoped instead of a Singleton.
Here are some details:
StructuremapMvc class in app_start folder
public static class StructuremapMvc
{
public static void Start()
{
IContainer container = IoC.Initialize();
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(container);
}
}
Ioc class
public static IContainer Initialize()
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.AssemblyContainingType<IConfigManager>();
scan.WithDefaultConventions();
scan.Convention<CustomConvention>();
});
CustomConvention : IRegistrationConvention
public void Process(Type type, Registry registry) public void Process(Type type, Registry registry)
{
var attributes = type.GetCustomAttributes(false);
if (attributes.Length > 0)
{
if (attributes[0] is SingletonAttribute)
{
registry.For(type).Singleton();
}
else if (attributes[0] is HttpContextScopedAttribute)
{
registry.For(type).HttpContextScoped();
}
}
}
[Singleton]
public class MyRegistry : IMyRegistry
This questions seems to be quite old but I'll trie to answer it anyway because there could be others which are experiencing the same problem with Structure map. In some cases singleton insances are created "per instance" referring to the instance where they are injected in. This means that you could have different instances of "singleton" when they are injected somewhere else. I've personally seen this behavior with WEBAPI inside MVC app.
The only way I could make it work as "true" global singleton is by using generic interface with specific type parameters to distinguish different types to be used:
public interface ITest<T>
{
}
public class Test1 : ITest<int>
{
}
public class Test2 : ITest<string>
{
}
Scan(x =>
{
x.TheCallingAssembly();
x.IncludeNamespace("MvcApplication1");
x.ConnectImplementationsToTypesClosing(typeof(ITest<>))
.OnAddedPluginTypes(a => a.LifecycleIs(InstanceScope.Singleton));
});
I know that this isn't as ellegant nor usable as approach described above but at least it works as expected. Other approach which works is to do standard mapping one-on-one like:
For<ISingleton>().Singleton().Use<Singleton>();

Convert .net user control to COM component

I have a working .net winform user control and would like to convert it to COM. I've been struggling finding a way to convert this user control to be COM enabled (for use in VS6/C++6). I'm not sure if this is do-able? Or I have to think about a different approach? Here's the interface:
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("C8BDB591-189D-4EB5-A026-7C9FFBEE3A85")]
public interface iMainInterface
{
[DispId(1)]
void ShowMyInterface();
}
And here's the control:
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(iMainInterface))]
[Guid("F8D26781-5A97-4467-B732-7EAB1A04C3F2")]
public partial class MainInterface : UserControl
{
public void ShowMyInterface()
{
...
}
}
The error message seems to be for [ComSourceInterfaces(typeof(iMainInterface))]
Here's the error:
Error 2 Cannot register assembly "MyInterface.dll". Type 'xxx.MainInterface' does not support the specified default COM interface: 'xxx.iMainInterface' PerformReportControl
[ComSourceInterfaces] should only be used for interfaces that generate events. It sure doesn't look like iMainInterface has any events so just remove the attribute.
You forgot to have your class inherit the interface. Fix:
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("F8D26781-5A97-4467-B732-7EAB1A04C3F2")]
public partial class MainInterface : UserControl, iMainInterface
{
public void ShowMyInterface()
{
...
}
}
Do favor a capital I (not i) for interface types.