Invalid Cast Exception ASP.NET C#
- by Shadow Scorpion
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?