views:

73

answers:

2

I have a UserControl (lets say Foo.ascx) that has a Type public property with the name Bar. I am looking for a way that when i declare this usercontrol in the source view of the markup part to pass a type. for example

<%@ Register Src="~/Controls/Foo.ascx" TagPrefix="prfx" TagName="fooCtrl" %>

and then use it as

<prfx:fooCtrl ID="theId" runat="server" />

if for example i wanted to pass to the control the type of string (such as typeof(string)) something that would have this effect

<prfx:fooCtrl ID="theId" runat="server" Bar="typeof(string)" />

how can it be done? Before anyone asks, the reason is that i have many other properties in this usercontrol that i pass in this manner and i want to avoid using the CodeBehind just to pass the Type

+1  A: 

Short answer, you can't do this. The editor and compiler treats .ascx files as (in the end) XML. That means on "deserialization" properties can be converted to various primitive types but not complex types such as System.Type. To work, what you would like to do would require, at some point during compilation, that an attribute in an xml document not be treated as text, not converted to a simple type, but interpreted and executed as code. Without altering how Visual Studio and the ASP.NET compiler works, this won't happen.


Workaround:

  1. Create a public property of type string in your UserControl
  2. Pass the AssemblyQualifiedName of the type you wish to pass to the UserControl via this attribute
  3. Use the GetType(string) overload to get an instance of this type within your code.

    <prfx:fooCtrl ID="theId" runat="server" Bar="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

and, in code:

/* ... */
var barType = Type.GetType(this.Bar);
// do whatever you want with this type
Will
check the comment i made to Anders Fjeldstad's answer
Jaguar
@jag that isn't going to happen. XML is a different beast, and this is XML. The only way to add intellisense to an xml document is to use an XSD, which would not work in your case. There is absolutely no way to add intellisense to this choice (without writing an extension for Visual Studio).
Will
You are making a mistake. Passing a Type UserControl property does not depend on Visual Studio's features. Even if i coded in notepad i would still not know (or be able) to pass a Type property. It is something that is not supported directly (or at least i don't know about it) and i am asking if it can be done. Now, if visual studio's intellisense can some how support it is actually a great benefit, but it is not the main idea. I am looking for a mechanism that will compile and work, the optional feature would be for it to help the developer via Visual Studio
Jaguar
@jag you don't understand the difference between code and text, and how VS handles them. You also don't understand the difference between the types of editors used in VS. And I'm not making any mistakes on this one. Thanks for playing.
Will
you are missing the point. i don't care (or anyone should) what visual studio does or does not understand. As i said, i could type the code in notepad and the compiler verifies if something is proper or not. Either way the question is if there is a way to do that - because obviously i don't know how to do it. Other than that, please answer the question if you can. If you cannot its ok, but don't derail the topic. Thanks for the discussion
Jaguar
@Jag ffs you're asking text in an xml file be interpreted by the CLR and executed like its friggen code. You can't do this. Its XML, not code. You'd have to change the xml editor and the asp.net compilers. I answered the question several times. Let me do it again: No. You got two answers which detail exactly how everybody else does this. You therefore have two choices--do it that way, or provide an addin for VS that eases the process.
Will
this is the first real answer: it cannot be done.Btw none of the answers point out what you _can't_ to. They just provide a workaround, and that is quite common in SO. Don't get aggravated by a discussion.
Jaguar
@jag lol updating my answer.
Will
+1  A: 

You could add a property of type string to your user control called for example TypeName:

public string TypeName { get; set; } // Validation omitted for brevity

Then change the implementation of your Bar property to:

public Type Bar { get { return Type.GetType(TypeName); } }

That way you could write:

<prfx:fooCtrl ID="theId" runat="server" Bar="System.String" />

Have all code that needs to use the type call the Bar property.

Anders Fjeldstad
that's what i'm doing now, i am searching for a compile-safe methodology, additionally, i state i want to avoid code-behind
Jaguar
Markup is text, so it will be difficult to pass a complex object such as a type and have it automatically instanciated without touching the code-behind.
Anders Fjeldstad
if i set a boolean property, intellisense will check for "true" or "false". i am looking for the same functionality but with Type types.
Jaguar