Pete,
No, it's not necessarily the only solution. However, unless you post a
concise-but-complete code example that reliably demonstrates exactly what
you're doing, I can only continue to make guesses as to what might or
might not work for you.
Thank you.
You quesses seems to be 100% presice for my case.
I think that if you want to only modify code in the EntityBase class
itself, by definition reflection would be your only possible solution,
since you've stated specifically that at no point during the call chain do
you have the actual type in hand (which is necessary for providing a
generic type parameter).
Sadly this seems to be true. My endpoint in web service generic Add method
which receives entity name as parameter.
Earlier you wrote that Add method name is wrong and suggested to find
correct generic method name. I tried code for this:
public abstract class EntityBase
{
void Add<TEntity>(object instance, object sender) where TEntity :
EntityBase
{ ....
}
public void CreateAndAdd(object sender)
{
var meth = GetType().GetMethods(BindingFlags.NonPublic |
BindingFlags.Instance);
var mi = GetType().GetMethod("Add", BindingFlags.NonPublic |
BindingFlags.Instance);
var gm = mi.MakeGenericMethod(GetType());
gm.Invoke(this, new object[] { sender });
....
}
I set breakpoint after meth variable assignment and watched its values in
Watch window.
Suprisingly meth variable does not contain *any Add* method. It contains 11
methods:
- meth {System.Reflection.MethodInfo[11]} System.Reflection.MethodInfo[]
+ [0] {MyApp.Business.QueryBuilder GetSql(MyApp.Business.Kontekst,
Eeva.Business.FormDefinition ByRef,MyApp.Business.FormLayout)}
System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
+ [1] {Void OnPropertyChanged(System.String)} System.Reflection.MethodInfo
{System.Reflection.RuntimeMethodInfo}
+ [2] {System.String GetFrom(MyApp.Business.FormDefinition,
Eeva.Business.FormLayout)} System.Reflection.MethodInfo
{System.Reflection.RuntimeMethodInfo}
+ [3] {System.String BaseTableAlias()} System.Reflection.MethodInfo
{System.Reflection.RuntimeMethodInfo}
+ [4] {System.Collections.Generic.IList`1[System.String]
GetSelectList(MyApp.Business.FormDefinition)} System.Reflection.MethodInfo
{System.Reflection.RuntimeMethodInfo}
+ [5] {System.String GetExpression(Eeva.Business.FormField)}
System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
+ [6] {System.Collections.Generic.IList`1[System.String]
DisplayOrder(Int32, System.String)} System.Reflection.MethodInfo
{System.Reflection.RuntimeMethodInfo}
+ [7] {Void AppendWhere(MyApp.Business.QueryBuilder,
Eeva.Business.Kontekst)} System.Reflection.MethodInfo
{System.Reflection.RuntimeMethodInfo}
+ [8] {System.String[] get_RequiredFields()} System.Reflection.MethodInfo
{System.Reflection.RuntimeMethodInfo}
+ [9] {Void Finalize()} System.Reflection.MethodInfo
{System.Reflection.RuntimeMethodInfo}
+ [10] {System.Object MemberwiseClone()} System.Reflection.MethodInfo
{System.Reflection.RuntimeMethodInfo}
How to fix this code so that Add<TEntity> can be called with real type ?
How to find Add<TEntity> method name for GetMethod ?
Andrus.