I’m working on a C# .NET solution with lots of re-usable assemblies. Three of these are:
- a WinForms assembly
- a webclient class library
- an assembly that contains the data model classes
I have a generic method in the class library as such:
namespace Company.WebClient {
public class GetData<T>()
{
...
}
}
However, when I call the method from within the WinForms assembly, I pass in a Type that the class library won’t know, since it’s contained in the data model assembly:
namespace Company.WinFormsApp {
public class App
{
public void Main()
{
Company.WebClient.GetData<TypeFromTheDataModel>();
}
}
}
Surprisingly, it seems to work. But why does it work? The webclient assembly has no hard-coded reference to the data model assembly so I’m surprised that it doesn’t report ‘type not found’ or some such error. Is this a safe way of working, or should I add more references to my project (i.e. from the class library to the data model)?
The library doesn’t need to know anything about the type – it’s not trying to use any members of the type, after all. At execution time
GetDatacould find out aboutT– but at compile-time it doesn’t need to.Just think – if this didn’t work, then LINQ to Objects would be completely broken, as you could only use it for sequences of system types!
Basically, it’s entirely safe to do this.