English 中文(简体)
2. 将财产类型移至通用方法
原标题:Get property type to pass to generic method

我需要获得一种财产类型,这种类型只有在操作时间才知道,并且作为通用方法的类型参数。 例如:

PropertyInfo prop = Bar.GetProperty("Property1");

//"type  prop  could not be found" error
Foo<prop.PropertyType>();

void Foo<T>()
{
   //Stuff
}
class Bar
{
   string Property1{get;set;}
}

律师资格的类型在编纂时将不为人所知,因此,我可以打上<编码>。 Foo<string>。 如果我使用<代码>,将进行正确汇编和操作。 Foo<dynamic>;,但I m不相信最佳途径,我很想知道,是否有办法利用较老的框架做到这一点。


Hopefully, this more complete example will make my intentions clearer:

    public void Map(TInType inObject, TOutType outObject)
    {
        //propertyIn, propertyOut, and converter are all strings identifying the properties/methods to be used from the inObject/Type outObject/Type.
        SetPropertyValues<dynamic, dynamic>(inObject, outObject, propertyIn, propertyOut, converter);
    }
    private void SetPropertyValues<TPropIn,TPropOut>(TInType fromObject, TOutType toObject, string propertyIn, string propertyOut, string converter)
    {
        PropertyInfo prop1 = typeof(TInType).GetProperty(propertyIn);
        MethodInfo converterMethod = typeof(TInType).GetMethod(converter);
        PropertyInfo prop2 = typeof(TOutType).GetProperty(propertyOut);

        prop2.SetValue(
            toObject,
            CopyPropertyValue<TPropIn, TPropOut>((TPropIn)prop1.GetValue(fromObject, null), p => (TPropOut)converterMethod.Invoke(fromObject, new object[] { p })),
            null);
    }
    private TPropOut CopyPropertyValue<TPropIn, TPropOut>(TPropIn InPropValue, Func<TPropIn, TPropOut> converterFunction)
    {
        return converterFunction(InPropValue);
    }

我对任何人可能提出的任何其他建议持开放态度,或者应当收回和击退该守则,但我的原始问题仍然是我最感兴趣的一个问题。

最佳回答

You can use MakeGenericMethod, performance is actually quite reasonable and allows you to explicitly define what you are calling with what, so reduces the overhead. So something like the following, the Invoker would call the explicit method / class you need, and the helper actually invokes the generic call.

public class GenericHelper
{
    public static void DoSomethingGeneric(GenericInvokerParameters parameters)
    {
        var targetMethodInfo = typeof(GenericInvoker).GetMethod("DoSomethingGeneric");
        var genericTargetCall = targetMethodInfo.MakeGenericMethod(parameters.InvokeType);
        genericTargetCall.Invoke(new GenericInvoker(), new[] { parameters });
    }
}

public class GenericInvoker
{
    public void DoSomethingGeneric<T>(GenericInvokerParameters parameters)
    {
        //Call your generic class / method e.g.
        SomeClass.SomeGenericMethod<T>(parameters.SomeValue);
    }
}

public class GenericInvokerParameters
{
    public GenericInvokerParameters(Type typeToInvoke, string someValue)
    {
        SomeValue = someValue;
        InvokeType = typeToInvoke;
    }

    public string SomeValue { get; private set; }
    public Type InvokeType { get; private set; }
}
问题回答

Don t see anything bad in dynamic. Use it.

http://www.ohchr.org。

如果你不再以高频率使用这一方法,那么从业绩的角度来看,我倾向于<条码>。

如果你不使用这种禁忌,那么这种禁忌就不应该是通用的。 仅此就根据的是类型目标而不是T。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签