c# - Convert system.Type to user defined type -
i trying classes inheriting base class.
public void getclassnames() { list<baseclass> data = appdomain.currentdomain.getassemblies() .selectmany(assembly => assembly.gettypes()) .where(type => type != null && type.issubclassof(typeof(baseclass))).tolist(); }
however, above code throws error.
"cannot implicitly convert type system.collections.generic.list<system.type>' to
system.collections.generic.list"
how cast type baseclass please?
you selecting types of assemblies sub-classes of baseclass
. types not instances of types.
what want? method name getclassnames
, maybe want:
public ienumnerable<string> getclassnames() { list<string> baseclassnames = appdomain.currentdomain.getassemblies() .selectmany(assembly => assembly.gettypes()) .where(type => type?.issubclassof(typeof(baseclass)) == true) .select(type => type.fullname) .tolist(); return baseclassnames; }
if instead want types in assemblies derive baseclass
:
public ienumnerable<type> getbaseclasssubtypesincurrrentassenblies() { list<type> baseclasstypes = appdomain.currentdomain.getassemblies() .selectmany(assembly => assembly.gettypes()) .where(type => type?.issubclassof(typeof(baseclass)) == true) .tolist(); return baseclasstypes; }
Comments
Post a Comment