J
Jim
Hi, I have a web control, that has a property (a collection of strings) with
it's own editor and typeconverter.
The problem is that the type converter has to ConvertTo an
InstanceDescriptor at runtime, but calls InstanceDescriptor (seem to) require
Unrestricted permission;
(permview /decl System.dll yields
Class System.ComponentModel.Design.Serialization.InstanceDescriptor
LinktimeDemand permission set:
<PermissionSet class="System.Security.PermissionSet"
version="1"
Unrestricted="true"/>
)
Also this page
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/aptcatypes.asp says;
The following is a list of types within these APTCA decorated assemblies
that are not callable by partially trusted code because they are decorated
with one of the following declarative security attributes:
[PermissionSet(SecurityAction.LinkDemand, Name="FullTrust")]
....for...
System.ComponentModel.Design.Serialization.InstanceDescriptor
I'm perplexed - CAS sounds like a nice idea but are you seriously telling me
that no-one but FullTrust users can use my code because I have a custom type
converter!
Is there anyway around this?
Thanks in advance
Jim
Here's my type converter
class MyControlsCollectionConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type
sourceType){
if (sourceType == typeof(string)) return true;
return false;
}
public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, object value) {
if (value == null)
return String.Empty;
try{
if (value.GetType() == typeof(string)){
string[] IDs = ((string)value).Split(new char[]{','});
MyControlsCollection r = new MyControlsCollection();
for(int i=0; i<IDs.Length; i++)
r.Add(IDs);
return r;
}
return null;
} catch (Exception f){
throw GetConvertFromException(value+f.Message);
}
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type
targetType){
if (targetType == typeof(string) || targetType ==
typeof(InstanceDescriptor)) return true;
else return base.CanConvertTo(context, targetType);
}
///<summary>Convert collection to string</summary>
public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, object value, Type targetType) {
if(targetType == typeof(string)){
string result = "";
MyControlsCollection list = null;
MyMainControl myMainControl = null;
try{
if(value!=null){
list = (MyControlsCollection) value;
if (context != null && context.Instance != null)
myMainControl = ((MyMainControl)context.Instance);
for(int i=0; i<list.Count; i++){
result += list;
if(i<list.Count-1) result+=",";
}
}
} catch (Exception e){
result="Internal error occured "+e.Message;
throw new Exception("Error occured;"+e.Message);
}
return result;
} else if (targetType == typeof(InstanceDescriptor)) {
//sometimes it wants to convert collection to instance descriptor, so
create an instance descriptor
//using string constructor
InstanceDescriptor desc = null;
ConstructorInfo ci = typeof(MyControlsCollection).GetConstructor(new
Type[]{typeof(string)});
MyControlsCollection t = (MyControlsCollection) value;
--------------->>>//fails because of this call
if (ci!=null) desc = CreateInstanceDescriptor(ci, t);
return desc;
} else return base.ConvertTo(context,culture,value,targetType);
}
InstanceDescriptor CreateInstanceDescriptor(ConstructorInfo ci,
MyControlsCollection t)
{
return new InstanceDescriptor(ci,new object[]{t.ToString()});
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext
context)
{return false;}
public override bool GetStandardValuesSupported(ITypeDescriptorContext
context)
{return false;}
}
it's own editor and typeconverter.
The problem is that the type converter has to ConvertTo an
InstanceDescriptor at runtime, but calls InstanceDescriptor (seem to) require
Unrestricted permission;
(permview /decl System.dll yields
Class System.ComponentModel.Design.Serialization.InstanceDescriptor
LinktimeDemand permission set:
<PermissionSet class="System.Security.PermissionSet"
version="1"
Unrestricted="true"/>
)
Also this page
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/aptcatypes.asp says;
The following is a list of types within these APTCA decorated assemblies
that are not callable by partially trusted code because they are decorated
with one of the following declarative security attributes:
[PermissionSet(SecurityAction.LinkDemand, Name="FullTrust")]
....for...
System.ComponentModel.Design.Serialization.InstanceDescriptor
I'm perplexed - CAS sounds like a nice idea but are you seriously telling me
that no-one but FullTrust users can use my code because I have a custom type
converter!
Is there anyway around this?
Thanks in advance
Jim
Here's my type converter
class MyControlsCollectionConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type
sourceType){
if (sourceType == typeof(string)) return true;
return false;
}
public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, object value) {
if (value == null)
return String.Empty;
try{
if (value.GetType() == typeof(string)){
string[] IDs = ((string)value).Split(new char[]{','});
MyControlsCollection r = new MyControlsCollection();
for(int i=0; i<IDs.Length; i++)
r.Add(IDs);
return r;
}
return null;
} catch (Exception f){
throw GetConvertFromException(value+f.Message);
}
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type
targetType){
if (targetType == typeof(string) || targetType ==
typeof(InstanceDescriptor)) return true;
else return base.CanConvertTo(context, targetType);
}
///<summary>Convert collection to string</summary>
public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, object value, Type targetType) {
if(targetType == typeof(string)){
string result = "";
MyControlsCollection list = null;
MyMainControl myMainControl = null;
try{
if(value!=null){
list = (MyControlsCollection) value;
if (context != null && context.Instance != null)
myMainControl = ((MyMainControl)context.Instance);
for(int i=0; i<list.Count; i++){
result += list;
if(i<list.Count-1) result+=",";
}
}
} catch (Exception e){
result="Internal error occured "+e.Message;
throw new Exception("Error occured;"+e.Message);
}
return result;
} else if (targetType == typeof(InstanceDescriptor)) {
//sometimes it wants to convert collection to instance descriptor, so
create an instance descriptor
//using string constructor
InstanceDescriptor desc = null;
ConstructorInfo ci = typeof(MyControlsCollection).GetConstructor(new
Type[]{typeof(string)});
MyControlsCollection t = (MyControlsCollection) value;
--------------->>>//fails because of this call
if (ci!=null) desc = CreateInstanceDescriptor(ci, t);
return desc;
} else return base.ConvertTo(context,culture,value,targetType);
}
InstanceDescriptor CreateInstanceDescriptor(ConstructorInfo ci,
MyControlsCollection t)
{
return new InstanceDescriptor(ci,new object[]{t.ToString()});
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext
context)
{return false;}
public override bool GetStandardValuesSupported(ITypeDescriptorContext
context)
{return false;}
}