Click here to Skip to main content
15,867,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I've got the following method:
C#
private void CreateSomething(Type pType)
{
    if (pType.BaseType != typeof(Window))
        throw new Exception("Wrong type.");
           
    ...
}

As I only want to call the method with types inherited from Window, an exception is thrown if the base type of pType isn't Window. In my opinion this solution isn't very nice.

Is there a possibility to change the signature of the method in that way that only specific types respectively inherited types of a given Type are allowed to call the method?


Thanks!
Gobble-G
Posted

I can see that you are hoping that there may be classes derived from Type
e.g. class WindowType : Type {} which could be used to provide some static type safety for your method but unfortunately there are not.


... This is not entirely true. Type is abstract - the compiler (or code generator) creates a implementation (a derivate of Type for each new type (class, struct, enum,...) defined by the program. These derivates are not explicitely accessible by the application, similar to anonymous types or types of anonymous delegates or lambdas.

You can enforce the compile type safety you require as follows:
// CreateSomething is poorly chosen name for something
// which returns 'void'
private void CreateSomething<AllowedType>() where AllowedType : System.Windows.Window 
{
 // no need to throw or check since compiler will refuse
 // to compile for any other call then CreateSomething<Window>
 // or any of its derivates

 // do your stuff
}


The client needs to define type for which the operation should be performed:
CreateSomething<System.Windows.Window>(); // ok

CreateSomething<System.Windows.NavigationWindow>(); // ok

CreateSomething<System.String>(); // compiler error!


Observe that you don't need to invoke GetType or typeof at all since the compiler does not even let you get that far.
 
Share this answer
 
v5
Comments
Paul Michalik 4-Sep-10 7:45am    
The and <> tags are driving me crazy - I had the reload and re-edit the post exactly 5 times to get these few lines of text right...!
We call it polymorphism (if I got you):
private void CreateSomething(Window derivedFromWindow)
{
  //..
}

:-)
 
Share this answer
 
Comments
William Winner 3-Sep-10 12:08pm    
Reason for my vote of 5
Yeah, that was what I was going to say.

For a nice example, I wrote up a little spell checker for textboxes. It is based on controls that inherit from TextBoxBase. So, my methods are written like:
Private void DoSomething(TextBoxBase myTextBox)
{
if(myTextBox is RichTextBox)
{
//RichTextBox Stuff
}
else if(myTextBox is TextBox)
{
//standard TextBox stuff
}
}
A Type object represents the run-time type of an instance based on the metadata associated with the object and there is nothing that can be done at compile-time to restrict the argument that may be assigned to your method.

I can see that you are hoping that there may be classes derived from Type
e.g. class WindowType : Type {} which could be used to provide some static type safety for your method but unfortunately there are not.

Alan.
 
Share this answer
 
Hi I think use generic method's .While calling the method give the specific type which u want.Hope it may help u.
 
Share this answer
 
Can't you use extension[^] method here?

C#
public static void CreateSomething(this Window window)
{
  ...   
}
 
Share this answer
 
Comments
William Winner 3-Sep-10 12:11pm    
Reason for my vote of 2
it didn't appear to me that the author was trying to add methods to existing types, but wanted to be able to pass in only the types that are inherited from a specific type. I don't think extension methods are what he was looking for.
Toli Cuturicu 5-Sep-10 6:14am    
Reason for my vote of 1
Not what the OP asked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900