Invalid Cast Exception in ASP.NET but not in WinForms
Posted
by Shadow Scorpion
on Stack Overflow
See other posts from Stack Overflow
or by Shadow Scorpion
Published on 2010-04-05T00:16:55Z
Indexed on
2010/04/05
1:13 UTC
Read the original article
Hit count: 399
I have a problem in this code:
public static T[] GetExtras <T>(Type[] Types)
{
List<T> Res = new List<T>();
foreach (object Current in GetExtras(typeof(T), Types))
{
Res.Add((T)Current);//this is the error
}
return Res.ToArray();
}
public static object[] GetExtras(Type ExtraType, Type[] Types)
{
lock (ExtraType)
{
if (!ExtraType.IsInterface)
return new object[] { };
List<object> Res = new List<object>();
bool found = false;
found = (ExtraType == typeof(IExtra));
foreach (Type CurInterFace in ExtraType.GetInterfaces())
{
if (found = (CurInterFace == typeof(IExtra)))
break;
}
if (!found) return new object[] { };
foreach (Type CurType in Types)
{
found = false;
if (!CurType.IsClass) continue;
foreach (Type CurInterface in CurType.GetInterfaces())
{
try
{
if (found = (CurInterface.FullName == ExtraType.FullName))
break;
}
catch
{
}
}
try
{
if (found)
Res.Add(Activator.CreateInstance(CurType));
}
catch { }
}
return Res.ToArray();
}
}
When I'm using this code in windows application it works! But I cant use it on ASP page. Why?
© Stack Overflow or respective owner