interface
How to check that type is inherited from some interface c#
No, is only works for checking the type of an object, not for a given Type. You want Type.IsAssignableFrom: if (attr != null && typeof(IInterface).IsAssignableFrom(type)) Note the order here. I find that I almost always use typeof(…) as the target of the call. Basically for it to return true, the target has to be the … Read more
Check if a generic T implements an interface
Generics, oddly enough, use extends for interfaces as well.1 You’ll want to use: public class Foo<T extends SomeInterface>{ //use T as you wish } This is actually a requirement for the implementation, not a true/false check. For a true/false check, use unbounded generics(class Foo<T>{) and make sure you obtain a Class<T> so you have a … Read more
When is using the C# ref keyword ever a good idea?
The Framework Design Guidelines (a book by Krzysztof Cwalina and Brad Abrams) recommend to avoid both ref and out parameters. AVOID using out or ref parameters. Using out or ref parameters requires experience with pointers, understanding how value types and reference types differ, and handling methods with multiple return values. Also, the difference between out … Read more
interface as return type
Yes, you can return an interface. Let’s say classes A and B each implement interface Ic: public interface Ic { int Type { get; } string Name { get; } } public class A : Ic { . . . } public class B : Ic . . . } public Ic func(bool flag) { … Read more
.NET: Unable to cast object to interface it implements
I hat the same problems with a library of mine providing “plugin”-functionality… I got it finally working… Here was my problem: I had one main assembly using plugins, one assembly with the plugin (Plugin.dll) AND (important) another assembly providing the plugin-functionality (Library.dll). The Plugin.dll referenced the main assembly (in order to be able to extend … Read more
Why must an C# interface method implemented in a class be public?
Here’s an example of why it doesn’t make sense to be able to override the visibility: interface someI { void doYourWork(); } public class A : someI { public void doYourWork() { //… } } public class B : someI { private void doYourWork() { //… } } void Main() { List<someI> workers = getWorkers(); … Read more
Joda-Time: DateTime, DateMidnight and LocalDate usage
I see advantages in having almost all the Interfaces using LocalDateTime (at the Service Layer at least) so that my Application doesn’t have to manage Timezones and can safely assume Times always in UTC. I’m not sure I understand your line of thinking here. LocalDateTime and DateTime represent two quite different concepts. It’s not the … Read more
Why are interfaces not [Serializable]?
Interfaces define a contract and do not have any state of their own. Serialization is about saving and loading state into and out of an object model. Not much point to serializing something that holds no state. To answer the practical question of forcing an implementation of an interface to be Serializable – this is … Read more
In Unity, how does Unity magically call all “Interfaces”?
When it comes to XXXUpdate, OnCollisionXXX and other MonoBehaviours, the way Unity registers is not reflection as it has been widely believed but some internal compilation process. HOW UPDATE IS CALLED No, Unity doesn’t use System.Reflection to find a magic method every time it needs to call one. Instead, the first time a MonoBehaviour of … Read more