As an example (and the reason of my question), the class Windows.XAML.Media.Transform, as far as I can see from the WinMD info shown by ILDASM, has no defined constructor.
But if I try to derive from that class, on my C# project, compiler complains that non constructor can be found.
This seems to me that this could be a result of hidden visibility of the constructor.
A same effect can be achieved in C# declaring a private (or internal) Constructor, but it must be declared, otherwise a public constructor is created by the compiler, and the class is indeed derivable.
Any hint?
I think what's going on here is, that the class has explicitly declared an internal (default) constructor, no public constructor(s), so the developers can inherit from the class within the defining assembly. Everybody else, outside the assembly is prevented from beeing able to inherit from the class.
WinRT is an evolution of the COM concepts and is a quite different story as we are talking about binary components and not source inheritance.
The most important thing in this ABI story is that the only things of the component that can be used are the ones exposed via interfaces.
Constructors cannot be defined in interfaces nor can statics, and this means that WinRT need interfaces for these too.
C# viewes of WinRT components are an artifact of the language projection and not the real layout of the component.
To fully understand what's there, you should look at the component in C++, using the native library WRL that is the library used to build the WinRT APIs.
The Constructor is a projection for the factory interface of the WinRT component (and statics, new to WinRT as they didn't exist in COM, have a similar treatment).
When you "new" from C# an object, a Factory component associated to the component is first created. After this the factory create the object.
For this reason the constructor question should be seen in terms of factory component and not in terms of constructor (that exist in the underlying implementation but it doesn't really matter as what you see of the component is only its binary contract, the ABI).
So there are different options:
1. A factory does not exist and you cannot create the component. If the Factory interface is internal you can't use it and can't create the object.
2. A factory exists and expose the default constructor. In C# you can new the object
3. A custom factory exists and expose custom constructors (whit parameters).
ITransformFactory is private and I believe this explain the behavior you have seen.
There is much more on this topic as WinRT aggregation is the way they provide binary inheritance and versioning, but this is another (very long) story.
Still digging, and never will stop, that's the fun part of our job :)
Related
I have a set of C++/CLI classes I've written as wrappers to native C++ classes. For the C++/CLI classes: I placed the constructor declarations inside the private section of the class declarations and did not implement them, however this leads to linker errors in every case where I have an unimplemented constructor.
What is the appropriate way to delete the default constructor in C++/CLI?
This works in native C++ because it uses a linker. Which is happy with a missing member implementation if nobody asks for it. But that doesn't work in managed code, it doesn't use a linker. Your class definition is copied into the assembly metadata and binding happens dynamically at runtime.
Which requires all declared members to have a representation, the metadata would otherwise be incomplete and that's not supported since that would break reflection. Even for private members. Nothing particularly difficult to solve, {} is a perfectly good implementation for a private constructor.
I placed the constructor and destructor declarations inside the private section of the class declarations and did not implement them, however this leads to linker errors in every case where I have an unimplemented constructor or destructor.
Just because you make the constructor private does not mean you no longer have to implement them. You're still calling it; you have to implement it.
I know that packages are collection of Classes and Interfaces, designed to both organize and classify its contents.
I also know that Classes are the definition of Objects, and the instructions for both them, their attributes/variables, and their functions/methods.
However, I have yet to understand what an Interface is, or what it is really for...
I have read this definition on Adobe's website..:
interface
Usage
interface InterfaceName [extends InterfaceName ] {}
Defines an interface. Interfaces are data types that define a set of methods; the methods must be defined by any class that implements the interface.
An interface is similar to a class, with the following important differences:
• Interfaces contain only declarations of methods, not their implementation. That is, every class that implements an interface must provide an implementation for each method declared in the interface.
• Interface method definitions cannot have any attribute such as public or private, but implemented methods must be marked as public in the definition of the class that implements the interface.
• Multiple interfaces can be inherited by an interface by means of the extends statement, or by a class through the implements statement.
Unlike ActionScript 2.0, ActionScript 3.0 allows the use of getter and setter methods in interface definitions.
...However, this is too vague to be helpful to me.
Does anyone know the purpose and proper implementation and/or design of an Interface in ActionScript?
Interfaces basically let you announce "This class can do these things."
As a real world example, you might want to make a tutorial for a game which highlights each of the different controls on screen one by one. Each control might flash or bounce to highlight itself, so you can say they implement an "IHighlightable" interface, and let them take care of the rest:
public interface IHighlightable {
function highlight():void;
}
Then in your controls:
public class Control implements IHighlightable {
public function highlight():void {
// Bounce and flash!
}
}
This means you can do things like:
private function highlightControl(tutorialItem:IHighlightable):void {
tutorialItem.highlight();
}
Importantly, you can have a class implement multiple interfaces, which is useful when classes share abilities, but it doesn't make sense to have them all extend a common base class.
Interfaces are a contract. It is compile time mechanism to force your to implement methods. In large OOP code bases, it is the best practice to have other classes depend on interfaces rather than other classes, so you can swap the implementation without changing the code that consumes the interface (this advantage is discutable in practice, since very often the interface will change too).
I believe interfaces are borrowed from Java which introduced them to achieve polymorphism (ClassA can be of type IFoo and IBar at the same time) without inheriting from multiple abstract classes (you can only extend one class, but you can implement any number of interfaces).
Although I'm technically wrong, I consider interfaces to be similar to C++ abstract classes.
Wikipedia article on Abstract Types
Oracle Java documentation on Abstracts
MSDN article on C# Interfaces
Interfaces are meant to define properties and methods without actually implementing them. Also, they cannot be instantiated
var test:FooInterface = new FooInterface() // would error
I'd like to be able to register some types on a container and then top these up with some additional type declared in an XML configuration file. Unfortunately, IConfigurationInterpreter (implemented by XmlInterpreter) is only available in the WindsorContainer() constructor, not in any AddXXX() methods. Is there any other way I can achieve this without resorting to parent/child containers (that may soon be unsupported).
Background: Our large application is only starting to use the Castle framework to register and resolve some of it's components. Because Castle is being retrofitted into this app we're using a singleton class to maintain a global instance of WindsorContainer(). In unit tests, we need to wire up this container instance to use a combination of custom mock implementations (specific to the test) + some default mock implementations. For DLL dependency reasons, these mock class types are unavailable in this unit test fixture abstract base class so dynamic registration (using strings) is necessary. I was hoping to use an XML resource file to register the default mocks. Otherwise I have to do the same using an IWindsorInstaller implementation that's really duplicating what XmlInterpreter does. This API appears to be forcing this direction.
I think these will work ...
container.Install(Castle.Windsor.Installer.Configuration.FromXml(resource))
OR
container.Install(Castle.Windsor.Installer.Configuration.FromXmlFile(path))
which both avoid use of the IConfigurationInterpreter interface.
I'm about to embark on a new project using Windsor, but I've been wondering about running into scenarios where a Class A might need to instantiate Class B, but it's not feasible or possible for Windsor to inject an instance of Class B into it. I'm struggling to think of a scenario, but here goes:
Say I have a business entity "Customer" that gets passed to a WCF service. This class has an Ent.Lib self-validation method, which in turn uses a helper class "CustomerValidator". The Customer object received by the service has been deserialized by WCF, so Windsor plays no part in its instantiation, so I can't inject any dependencies. Nor can I pass my CustomerValidator to the self-validation method as it must follow a particular signature for Ent.Lib. So how could I instantiate the CustomerValidator within this class/method? I still want to utilise Windsor rather than simply doing a "var cv = new CustomerValidator();".
It's not a great example as it could be solved in different ways, e.g. passing the Customer object to a validation method rather than having the validation method in the Customer class, but it offers a possible scenario for discussion.
I could expose my WindsorContainer as a public singleton, accessible by any code that needs it, but that seems to be frowned upon. Any other suggestions?
should I reference the container from other classes?
No. By referencing the container, you add complicated and unnecessary dependency to your class which will complicate testing and increase complexity.
The Customer object received by the service has been deserialized by WCF, so Windsor plays no part in its instantiation, so I can't inject any dependencies.
I think this is the direction you should go, try to explore if there really isn't any way to take control of deserialization so you can inject dependencies.
If that fails, consider using http://commonservicelocator.codeplex.com/. Its Microsoft's service location implementation with Windsor adapter available. It's basically the same pattern as if you referenced the container but you don't introduce dependency on specific container implementation. Also I think it will be easier to mock for testing.
I was wondering the differences between abstract and interface in actionscript 3 and when to use them..I have searched google but still couldn't understand them....I hope someone here can give me few tips...Thanks a lot!!!
The difference is that interface is valid actionscript, but abstract is not...
Now, in other languages you can mark a method or a class as abstract. This is somewhat like an interface, in that abstract means it has no implementation (for a method) or cannot be instantiated (for a class).
So, if a class is abstract, it means you cannot create an instance diretly (with new), but rather you have to extend the class to access its functionality.
An abstract method is pretty much like a method defined in an interface. When you extend a class that declares an abstract method, you have to provide an implementation that has the same signature, or your code won't compile. An abstract method makes the class abstract automatically, because otherwise you would be able to create an instance of an object that has an unimplemented method.
With an abstract class, you have some of the features of an interface (i.e. you define a method whose concrete implementation has to be provided) but you also can have other methods that are implemented and ready to use.
(This is a general explanation; maybe this is bit different in language X, but I think this gives you the basic idea)