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.
Related
public partial class CTMSEntitiesModel : OpenAccessContext, ICTMSEntitiesModelUnitOfWork
{
public CTMSEntitiesModel(string connection)
:base(connection, backend, metadataSource)
{ }
// there are more IQueryable requests here
}
public interface ICTMSEntitiesContext : ICTMSEntitiesModelUnitOfWork
{
FetchStrategy FetchStrategy { get; set; }
}
public interface ICTMSEntitiesModelUnitOfWork : IUnitOfWork
{
//all the IQueryable requests are here
}
I need to bind the ICTMSEntitiesContext to CTMSEntitiesModel. How would I go about doing that? What am I doing wrong when I do this? It is throwing an InvalidCastException.
kernel.Bind(typeof(CTMSDAL.ICTMSEntitiesContext)).To(typeof(CTMSDAL.CTMSEntitiesModel)).InRequestScope()
.WithConstructorArgument("connection", System.Configuration.ConfigurationManager.ConnectionStrings["CTMS_MVCConnection"].ConnectionString);
I would appreciate all the help you can provide!
Thanks,
Safris
You have to implement the ICTMSEntitiesContext in the CTMSEntitiesModel class. Otherwise there is no way to cast an instance of the class to the target interface.
Given that you are using OpenAccess and the fact that the context class may be automatically generated I would suggest to you add the interface implementation into a new partial class in different project file to avoid losing the custom code after the original file is regenerated:
public partial class CTMSEntitiesModel : ICTMSEntitiesContext
{
// FetchStrategy property is already defined
}
I am trying to find a way to have a factory class / method that would take in an object or some kind of identifier (string or type) then based off the input parameter determine which implementation of the interface to create and return.
how do I setup my factory method and register the dependency for the interface? following is what I have roughly.
public class ISampleFactory
{
public ISample GetSample(Type type)
{
// do something here to return an implementation of ISample
}
}
public class SampleA : ISample
{
public void DoSomething();
}
public class SampleB : ISample
{
public void DoSomething();
}
public interface ISample
{
void DoSomethin();
}
Have a look at ninject Contextual Bindings Documentation:
You can either use Named Bindings:
this.Bind<ISample>().To<SampleA>().Named("A");
this.Bind<ISample>().To<SampleB>().Named("B");
or a conditional binding with any of the already available extensions or write your own:
this.Bind<ISample>().To<SampleA>().When(...);
this.Bind<ISample>().To<SampleB>().When(...);
see https://github.com/ninject/ninject/wiki/Contextual-Binding
I created a LoggerDomainService witch look like this :
[EnableClientAccess()]
public class LoggerDomainService : DomainService
{
public void info()
{
// todo
}
}
And after building the server side the LoggerDomainContext did not created.
I've noticed that in order to make it work I need to declare inside the class at least on method with the [Query] attribute.
Is there better way to solver the problem, should I inherit from something else then DomainService ?
You are correct about the [Query] attribute. With your logging service, I recommend using [Invoke]:
[EnableClientAccess()]
public class LoggerDomainService : DomainService
{
[Invoke]
public void info()
{
// todo
}
}
Then you will find the context created.
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.
How do I solve the serialization problem with abstract class defined in a shared client library, and concrete implementation in a server side library.
Interface in shared client library :
[ServiceContract(SessionMode=SessionMode.Required)]
[ServiceKnownType(typeof(SharedClient.Shape))]
public interface IMyInterface
{
void UploadDrawing(Drawing dr);
}
Concreate Drawing class in shared client library :
[DataContract]
[KnownType(typeof(SharedClient.Shape))]
public class Drawing
{
public Shape s;
}
Abstract class in shared client library :
[DataContract]
abstract public class Shape
{
[DataMember]
public abstract string Name;
}
Concrete class implementation in separate library which references the client library :
[DataContract]
public class Cirle : ClientLibrary.Shape
{
public override string Name { get; set; }
}
I keep getting the exception message:
There was an error while trying to serialize parameter
http://tempuri.org/:Drawing. The InnerException message was 'Type
'Circle' with data contract name
'Circle:http://schemas.datacontract.org/2004/07/' is not expected.
Consider using a DataContractResolver or add any types not known
statically to the list of known types - for example, by using the
KnownTypeAttribute attribute or by adding them to the list of known
types passed to DataContractSerializer.'. Please see InnerException
for more details.
KnownType works in other way. If you want to use KnownType attribute you must use it on the base class to define its child:
[DataContract]
[KnownType(typeof(Circle))]
abstract public class Shape
{
[DataMember]
public abstract string Name;
}
That will not be too much helpful in your case. Try to put ServiceKnownType with concrete class on your interface:
[ServiceContract(SessionMode=SessionMode.Required)]
[ServiceKnownType(typeof(Circle))]
public interface IMyInterface
{
void UploadDrawing(Drawing dr);
}
You doesn't have to define Shape as ServiceKnownType - it is already known because it is used in Drawing but WCF yet doesn't know the Circle type.