This question already has answers here:
How to find out which interfaces a .net class implements?
(4 answers)
Closed 4 years ago.
Is there a way to get a list of all interfaces of a C# class in the Visual Studio UI without digging through the superclass chain step by step? If not even a list on MSDN would be useful.
For example I can't see that Form is IDisposable without digging down to Control.
GetInterfaces:
typeof(List<string>).GetInterfaces()
returns
Type[] (8 items)4
typeof(IList<String>)
typeof(ICollection<String>)
typeof(IEnumerable<String>)
typeof(IEnumerable)
typeof(IList)
typeof(ICollection)
typeof(IReadOnlyList<String>)
typeof(IReadOnlyCollection<String>)
If you need to know a particular interface, you can use IsAssignableFrom:
typeof(ICollection).IsAssignableFrom(typeof(List<string>))
returns true
Related
This question already has answers here:
Type Checking: typeof, GetType, or is?
(15 answers)
Checking if the object is of same type
(4 answers)
How can I check if an object is of a certain type at runtime in C#?
(9 answers)
Closed 3 years ago.
I was wondering if there is a way to find if my interface reference is a specific class.
For example i have DeviceInterface reference, and Playstation, PC and Mac all implement it. Is there a way to see if DeviceInterface is a PC?
I have thought about using a enum to define the type and using that, but is there a way of avoiding this and using a type check or something along those lines?
Thanks in advance.
Let's say that you have
DeviceInterface PcDevice = new PC();
In that case you can just do:
if (PcDevice is PC) { console.WriteLine("I'm a PC"); }
read more here to understand the is and as operators better
This question already has answers here:
Application.ProductName equivalent in WPF?
(7 answers)
Closed 8 years ago.
Is there something similar to the following property in WPF?
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.productname(v=vs.110).aspx
Thanks.
Not that I know of. But you can easily create this method yourself.
The actual code used by WinForms has several levels of fallback: it first look for an AssemblyProductAttribute on the assembly defining the control, then at the file version and finally falls back to the first part of the namespace.
You can copy that logic (or the parts that are relevant to you) directly from .net source code: http://referencesource.microsoft.com/#System.Windows.Forms/ndp/fx/src/winforms/Managed/System/WinForms/Control.cs#f7c944851a004a6e
This question already has answers here:
WPF version of Application.OpenForms
(3 answers)
Closed 8 years ago.
I have a small wpf/c# application and want to get a list of all of the open views. However, the Application.OpenForms, does not have "OpenForms" in the intellisense (and gives me an error). Could I be missing a specific reference of something?
I have tried: System.Windows.Application.OpenForms;
Based on the docs for the Application class, there is no OpenForms member.
http://msdn.microsoft.com/en-us/library/system.windows.application(v=vs.110).aspx
Try Application.Windows
var openWindows = System.Windows.Application.Current.Windows;
This question already has answers here:
How do arrays in C# partially implement IList<T>?
(6 answers)
Closed 8 years ago.
I have a class constructor that takes a IList<IElement> as an argument.
When creating a new instance of the class I'm able to pass a IElement[] instead of the IList<IElement> how is that posible?
An array with element type T derives from IList<T>.
This is not visible in the meta-data in mscorlib.dll, but the inheritance relationship is created at runtime in the CLR. C# and the CLR are aware of the array type and treat it specially.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Try to describe polymorphism as easy as you can
What is polymorphism?
Please read MSDN which covers it in reference to c#,
Basically a derived class inherits from another class it gets all its methods,events and properties, and every type is polymorphic in .NET since they all have Object as their base class.