Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys

i have a class that build in runtime(RClass) and also have a Generic class DataLoader, how can i create a type of DataLoader<RClass> and use its methods

C#
public interface IDataLoader<GType>
{
//some code
   GType SampleMethod();
}

public class DataLoader <GType>: IDataLoader<GType>
{
  GType SampleMethod(){
    //some code
  }
}

//class "RClass" created in runtime
MyClassBuilder MCB=new MyClassBuilder("RClass");
var myclass = MCB.CreateObject(new string[3] { "id", "name", "link" }, new Type[3] { typeof(int), typeof(string), typeof(string) });
Type myclassType = myclass.GetType();

var dta = typeof(DataLoader<>).MakeGenericType(myclassType);
// ?????
var gs = (???)Activator.CreateInstance(dta);
gs.SampleMethod();
Posted
Updated 16-May-15 8:34am
v5
Comments
Sergey Alexandrovich Kryukov 15-May-15 17:13pm    
What would it supposed to mean, "send a class"?
—SA
eagers 15-May-15 17:28pm    
thanks for the answer, i improve the question
Sascha Lefèvre 15-May-15 22:33pm    
It's exactly the same problem you already got an answer for here:
http://www.codeproject.com/Questions/991807/How-can-Create-two-nested-class-in-runtime?arn=0

Just B<T> instead of List<T> :-)
eagers 16-May-15 3:48am    
thanks for the answer, i'v got that what you say, so i must have an interface for my generic class somthing like IB like IList and use that technique in http://www.codeproject.com/Questions/991807/How-can-Create-two-nested-class-in-runtime?arn=0
Maciej Los 16-May-15 7:02am    
Do not repost!!![^]

1 solution

I think what you want to use would be something like:
C#
Type BTP = typeof(B).MakeGenericType(TP);
System.Reflection.ConstructorInfo ctorBTP = BTP.GetConstructor(Type.EmptyTypes);
dynamic a = ctorBTP.Invoke(null);

There's no way I can think of to have this be strongly typed at compile time as B<TP> because that type doesn't exist at compile time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900