on 03-08-2006 12:57 PM
We have inhertited classes from Destination, SAPLogonDestination.
How we would tell CombinedDestination's TypeConverter to use the other inherited classes .
Thanks for your reply.
It is very helpful. When i complie the code as per your guidance and code. It will generate the error on Instance Descriptor class and new InstanceDescriptor(createFunc , pars)
Can you send InstanceDescriptor class which will solve my problem
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for help
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Windows.Forms;
using System.Data;
using SAP.Connector;
namespace PropertyGridConnectionString
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PropertyGrid propertyGrid1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.SuspendLayout();
//
// propertyGrid1
//
this.propertyGrid1.CommandsVisibleIfAvailable = true;
this.propertyGrid1.LargeButtons = false;
this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid1.Location = new System.Drawing.Point(0, 0);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.Size = new System.Drawing.Size(288, 240);
this.propertyGrid1.TabIndex = 0;
this.propertyGrid1.Text = "propertyGrid1";
this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid1.ViewForeColor = System.Drawing.SystemColors.WindowText;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 253);
this.Controls.Add(this.propertyGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
propertyGrid1.SelectedObject=new AdvanceLogon();
}
}
public class AdvanceLogon : CombinedDestination
{
[TypeConverter(typeof(DestinationTypeListConverter))]
public new Type DestinationType
{
get
{
return base.DestinationType;
}
set
{
base.DestinationType = value;
}
}
}
internal class DestinationTypeListConverter : TypeConverter
{
private Hashtable DestinationTypes = new Hashtable();
public DestinationTypeListConverter()
{
System.Reflection.Assembly ass = typeof(SAP.Connector.SAPClient).Assembly;
foreach(Type t in ass.GetExportedTypes())
{
if(t.IsClass && (null != t.GetInterface("IDestination")))
{
if(t != typeof(CombinedDestination))
{
object[] atts = t.GetCustomAttributes(typeof(DescriptionAttribute), false);
if((atts != null) && (atts.Length >= 1))
{
string name = ((DescriptionAttribute)atts[0]).Description;
DestinationTypes.Add(name,t);
}
}
}
}
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if(sourceType == typeof(string))
return true;
else
return base.CanConvertFrom(context,sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if(value.GetType() == typeof(string))
{
if(this.DestinationTypes.Contains(value))
return DestinationTypes[value];
else
return null;
}
else
return base.ConvertFrom (context, culture, value);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if((destinationType == typeof(string)) || (destinationType == typeof(InstanceDescriptor)))
return true;
else
return base.CanConvertTo (context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
Type t = (Type)value;
if(destinationType == typeof(string))
{
object[] atts = t.GetCustomAttributes(typeof(DescriptionAttribute),false);
if((atts != null) || (atts.Length >= 1))
return ((DescriptionAttribute)atts[0]).Description;
else
return t.Name;
}
else if(destinationType == typeof(InstanceDescriptor))
{
Type[] signature = new Type[1];
signature[0] = typeof(string);
System.Reflection.MemberInfo createFunc = typeof(Type).GetMethod("GetType" , signature);
if (createFunc != null)
{
object[] pars = new object[1];
pars[0] = t.AssemblyQualifiedName;
return new InstanceDescriptor(createFunc , pars);
}
return null;
}
else
return base.ConvertTo (context, culture, value, destinationType);
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return base.GetStandardValues (context);
}
}
public class mySapLogonDestination : SAPClient
{
[Browsable(false)]
public override string ConnectionString
{
get
{
return ConnectionString;
}
}
}
}
I have done above code according to your direction. When I build the solution, I got following error
Referenced class 'SAP.Connector.SAPClient' has base class or interface 'System.Web.Services.Protocols.SoapHttpClientProtocol' defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web.Services'.
If anything wrong in my approach, Please guide accordingly.
Regards
Kumar Prabhakar
After adding System.Web.dll and System.Web.Services.dll, it compiled successfully. DestinationType combobox has no value. I have define two custom class derived from Destination and SAPLogonDestination
public class myLogonDestination : Destination
{
[Browsable(false)]
public override string ConnectionString
{
get
{
return ConnectionString;
}
}
}
public class mySapLogonDestination : SAPLogonDestination
{
[Browsable(false)]
public override string ConnectionString
{
get
{
return ConnectionString;
}
}
How to add these two derived class into DestinationType? when we choose our own custom class from destinatio type, It will change the internal class as well as hide the ConnectionString property of internal
Regards
Kumar Prabhakar
Of cause you need to fine-tune your TypeConverter to recognise your destiantion types instead of mine. E.g. you change the assembly where the TypeConverter searches for IDestination-implementing types to your one. Also you need to specify a [Description()] attribute to your destinations, because my TypeConverter uses its value to convert from/to strings.
It should help you to set a breakpount into the methods of the TypeConverter and step to them in debugger to understand what exactly happens there.
Just replace the DestinationType property with a new one and write a new TypeConverter for it:
[TypeConverter(typeof(NewDestinationTypeListConverter))]
public new Type DestinationType
{
get
{
return base.DestinationType;
}
set
{
base.DestinationType = value;
}
}
As a starting point for your work I post the source code of my TypeConverter:
internal class DestinationTypeListConverter : TypeConverter
{
private Hashtable destinationTypes = new Hashtable();
public DestinationTypeListConverter()
{
System.Reflection.Assembly ass = typeof(SAP.Connector.SAPClient).Assembly;
foreach(Type t in ass.GetExportedTypes())
{
if (t.IsClass && (null != t.GetInterface("IDestination")))
{
if (t != typeof(CombinedDestination))
{
object[] atts = t.GetCustomAttributes(typeof(DescriptionAttribute), false);
if ((atts != null) && (atts.Length >= 1))
{
string name = ((DescriptionAttribute)atts[0]).Description;
destinationTypes.Add(name, t);
}
}
}
}
}
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
{
if (sourceType == typeof(string))
return true;
else
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value.GetType() == typeof(string))
{
if (this.destinationTypes.Contains(value))
return destinationTypes[value];
else
return null;
}
else
return base.ConvertFrom(context, culture, value);
}
public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type destinationType)
{
if ((destinationType == typeof(string)) || (destinationType == typeof(InstanceDescriptor)))
return true;
else
return base.CanConvertFrom(context, destinationType);
}
public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
{
Type t = (Type) value;
if (destinationType == typeof(string))
{
object[] atts = t.GetCustomAttributes(typeof(DescriptionAttribute), false);
if ((atts != null) && (atts.Length >= 1))
return ((DescriptionAttribute)atts[0]).Description;
else
return t.Name;
}
else if (destinationType == typeof(InstanceDescriptor))
{
Type[] signature = new Type[1];
signature[0] = typeof(String);
System.Reflection.MethodInfo createFunc = typeof(Type).GetMethod("GetType", signature);
if (createFunc != null)
{
Object[] pars = new Object[1];
pars[0] = t.AssemblyQualifiedName;
return new InstanceDescriptor(createFunc, pars);
}
return null;
}
else
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool GetStandardValuesSupported(System.ComponentModel.ITypeDescriptorContext context)
{
return true;
}
public override bool GetStandardValuesExclusive(System.ComponentModel.ITypeDescriptorContext context)
{
return true;
}
public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(System.ComponentModel.ITypeDescriptorContext context)
{
return new System.ComponentModel.TypeConverter.StandardValuesCollection(this.destinationTypes.Values);
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
83 | |
24 | |
12 | |
9 | |
7 | |
6 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.