I have a custom server control for asp.net. One of the properties available to it is a generic object. That generic object has custom attributes that are read and used to customize the rendering of the control.
At run time, this is no problem, since all I need to do is find all the attributes on the instance that gets passed into the control.
However, just as the "DataSource" member of controls can call up a list of object types at design time, I would like to be able to bring up a list of types in the host assembly. There is no need for actual instances, since I'm rendering based on attributes.
This is an example property from the host control:
[Browsable(true)]
[TypeConverter(typeof(TheObjectConverter))]
public object TheObject
{
get { return _theObject; }
set { SetTheObject(value); }
}
What I want to have happen is to have TheObjectConverter be able to load the assembly of the host page for the control.
I even a full recursion against .GetReferencedAssemblies(), with no luck. It seems I am unable to load the assembly of the control's host at design time. Is that correct, or is there a way to do this?
Hi,
What you want to know is the way to have type information of certain type (as objectconverter) from host page of your custom control. Is it correct? If so, I think I can help. Please see the following code.
// you may input objecttypeconverter's typeinfo in argument when you call this.
private List<string> GetTargettClassNameList(Type compareType)
{
List<string> retList = new List<string>();
Assembly crrAssembly = Assembly.GetExecutingAssembly();
Module[] mods = crrAssembly.GetModules();
foreach (Module md in mods)
{
Type[] typeArray = md.GetTypes();
foreach (Type myType in typeArray)
{
if (myType.IsSubclassOf(compareType))
{
retList.Add(myType.FullName);
}
}
}
return retList;
}
Above code shows the way to get type info inside current working assemblies. I'm afraind it might meet the point of your question. If you also want to get type information from assembly to which host application have reference, see the following.
private List<string> GetTargettClassNameListFromReference(
Type compareType, IWebApplication argWebApp
)
{
List<string> retList = new List<string>();
string binFoler = argWebApp.RootProjectItem.PhysicalPath + @"\bin";
DirectoryInfo di = new DirectoryInfo(binFoler);
if (di.Exists)
{
foreach (FileInfo fi in di.GetFiles())
{
Assembly dllAssembly = Assembly.LoadFrom(fi.FullName);
Module[] dllMods = dllAssembly.GetModules();
foreach (Module md in dllMods)
{
Type[] typeArray = md.GetTypes();
foreach (Type myType in typeArray)
{
if ((myType.IsClass) && (myType.IsPublic) &&
(myType.IsSubclassOf(compareType))
{
retList.Add(myType.FullName);
}
}
}
}
}
return retList;
}
I can't tell it's a normal way or not, however it works. (Someone please show us a formal way!).
Hope it might help you. Good-luck!
Unfortunately, this does not allow me to get the assembly which is currently being designed in Visual Studio designer -- the assembly hosting the control. I think there may simply be no way to do this.
In this program, I prepared custom server control "ConverterViewer" which shows the
list of Converter collected from current working assembly.
When you open test form "ConverterNameTest.aspx" in design mode, choose "Converters" property in property grid, click reference button, then Windows Dialog Form will pop up.
In that modal dialog, you'll find the list of converters inside current assembly. (These converters are defined inside inner folder of App_Code") Inside the lower listbox, those which are already selected as property are shown.
The place I collect assembly information is around line 254 of "ConverterInfo.cs" Source file. Method name is "GetCandidateConverterList" which is called from EditValue method of custom UITypeEditor.
I hope, it may help you solving your problem. Or at least it might give you any kind of progress. If not, please forget it.
I suppose I should have specified that my controls are compiled in a separate projct (dll library), and are not part of App_Code, which may make a difference.
90ni1823
0 Points
3 Posts
Is it possible for a custom asp.net control to load types at design time from the assembly hostin...
Sep 12, 2009 02:46 AM|LINK
I have a custom server control for asp.net. One of the properties available to it is a generic object. That generic object has custom attributes that are read and used to customize the rendering of the control.
At run time, this is no problem, since all I need to do is find all the attributes on the instance that gets passed into the control.
However, just as the "DataSource" member of controls can call up a list of object types at design time, I would like to be able to bring up a list of types in the host assembly. There is no need for actual instances, since I'm rendering based on attributes.
This is an example property from the host control:
[Browsable(true)] [TypeConverter(typeof(TheObjectConverter))] public object TheObject { get { return _theObject; } set { SetTheObject(value); } }What I want to have happen is to have TheObjectConverter be able to load the assembly of the host page for the control.
None of the following seem to work
I even a full recursion against .GetReferencedAssemblies(), with no luck. It seems I am unable to load the assembly of the control's host at design time. Is that correct, or is there a way to do this?
assembly reflection "design time"
Satokun-Papa
Member
15 Points
14 Posts
Re: Is it possible for a custom asp.net control to load types at design time from the assembly ho...
Sep 13, 2009 04:41 PM|LINK
Hi,
What you want to know is the way to have type information of certain type (as objectconverter) from host page of your custom control. Is it correct? If so, I think I can help. Please see the following code.
// you may input objecttypeconverter's typeinfo in argument when you call this. private List<string> GetTargettClassNameList(Type compareType) { List<string> retList = new List<string>(); Assembly crrAssembly = Assembly.GetExecutingAssembly(); Module[] mods = crrAssembly.GetModules(); foreach (Module md in mods) { Type[] typeArray = md.GetTypes(); foreach (Type myType in typeArray) { if (myType.IsSubclassOf(compareType)) { retList.Add(myType.FullName); } } } return retList; }Above code shows the way to get type info inside current working assemblies. I'm afraind it might meet the point of your question.
If you also want to get type information from assembly to which host application have reference, see the following.
private List<string> GetTargettClassNameListFromReference( Type compareType, IWebApplication argWebApp ) { List<string> retList = new List<string>(); string binFoler = argWebApp.RootProjectItem.PhysicalPath + @"\bin"; DirectoryInfo di = new DirectoryInfo(binFoler); if (di.Exists) { foreach (FileInfo fi in di.GetFiles()) { Assembly dllAssembly = Assembly.LoadFrom(fi.FullName); Module[] dllMods = dllAssembly.GetModules(); foreach (Module md in dllMods) { Type[] typeArray = md.GetTypes(); foreach (Type myType in typeArray) { if ((myType.IsClass) && (myType.IsPublic) && (myType.IsSubclassOf(compareType)) { retList.Add(myType.FullName); } } } } } return retList; }I can't tell it's a normal way or not, however it works. (Someone please show us a formal way!).
Hope it might help you. Good-luck!
90ni1823
0 Points
3 Posts
Re: Is it possible for a custom asp.net control to load types at design time from the assembly ho...
Sep 13, 2009 11:32 PM|LINK
Unfortunately, this does not allow me to get the assembly which is currently being designed in Visual Studio designer -- the assembly hosting the control. I think there may simply be no way to do this.
Satokun-Papa
Member
15 Points
14 Posts
Re: Is it possible for a custom asp.net control to load types at design time from the assembly ho...
Sep 14, 2009 03:10 PM|LINK
Hi,
I have uploaded sample web program which, I hope, may help you.
Please download it from
http://www.ad.il24.net/~masa_cmt/Samples/DesignTimeTest01.zip
In this program, I prepared custom server control "ConverterViewer" which shows the list of Converter collected from current working assembly.
When you open test form "ConverterNameTest.aspx" in design mode, choose "Converters" property in property grid, click reference button, then Windows Dialog Form will pop up.
In that modal dialog, you'll find the list of converters inside current assembly. (These converters are defined inside inner folder of App_Code") Inside the lower listbox, those which are already selected as property are shown.
The place I collect assembly information is around line 254 of "ConverterInfo.cs" Source file. Method name is "GetCandidateConverterList" which is called from EditValue method of custom UITypeEditor.
I hope, it may help you solving your problem. Or at least it might give you any kind of progress. If not, please forget it.
Good luck.
90ni1823
0 Points
3 Posts
Re: Is it possible for a custom asp.net control to load types at design time from the assembly ho...
Sep 17, 2009 08:36 AM|LINK
I suppose I should have specified that my controls are compiled in a separate projct (dll library), and are not part of App_Code, which may make a difference.