简体   繁体   English

C#private T CreateObject <T> ()

[英]C# private T CreateObject<T>()

i want to create an instance of object with a dynamic parameter like 我想用动态参数创建一个对象实例

private Type ClassType { get; set; }
model = (CreateObject<typeof(this.ClassType)>)ser.Deserialize(sr);


private T CreateObject<T>()
{
    return (T)Activator.CreateInstance(this.ClassType);
}

i want to try it without a fix Type like "startconfig". 我想尝试没有修复类型,如“startconfig”。 but it still doesnt work, can u help me? 但它仍然无法工作,你能帮帮我吗?

        var mi = GetType().GetMethod("CreateObject");
        var miConstructed = mi.MakeGenericMethod(this.ClassType);
        var instance = miConstructed.Invoke(this, null);
        var model = (instance)ser.Deserialize(sr);
    }

    private T CreateObject<T>()
    {
        return (T)Activator.CreateInstance(this.ClassType);
    }

this doesnt work anyway, cause: he type or namespace name 'type/namespace' could not be found (are you missing a using directive or an assembly reference?) 这无论如何都不起作用,原因:无法找到类型或命名空间名称'type / namespace'(你是否缺少using指令或程序集引用?)

that happends at casting the ser.Deserialize(sr); 在铸造ser.Deserialize(sr)时发生的事情;

You wanna create an instance of T ? 你想创建一个T的实例吗? Then : 然后 :

var model = CreateObject<StartConfig>();    

private T CreateObject<T>()
{
    return (T)Activator.CreateInstance(typeof(T));
}

with your second code sample, you might do something like that. 使用您的第二个代码示例,您可能会执行类似的操作。

private Type ClassType { get; set; }
var mi = GetType().GetMethod("CreateObject");
var miConstructed = mi.MakeGenericMethod(ClassType);
var instance = miConstructed.Invoke(this, null);
model = (instance)ser.Deserialize(sr);

private T CreateObject<T>()
{
    return (T)Activator.CreateInstance(typeof(T));
}

Try... 尝试...

public class Factory<T>
{
    public static T getInstance()
    {
        return getInstance(typeof(T), null);
    }

    public static T getInstance(object[] initializationParameters)
    {
        return (T)Activator.CreateInstance(typeof(T), initializationParameters);
    }
{

What do you want to do with your model ? 你想对你的model做什么? I mean interface-wise. 我的意思是界面方面。 You have to define an interface which all Types adhere to that you deserialize. 您必须定义一个所有类型都遵循您反序列化的接口。

public interface IModel
{
     int ComputeFavoriteNumber(); // or a property
}

...

// class is practically unknown to deserializing module
internal class ErnieModel : IModel
{
    public int ComputeFavoriteNumber()
    {
        return 8243721;
    }
}

...

// deserializing module
var bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
    bf.Serialize(ms, new ErnieModel()); // In reality ErnieModel should be unknown to the deserializing code, this is just to fill the Stream with data
    ms.Position = 0;
    var model = (IModel)bf.Deserialize(sr);
    Console.WriteLine("Favorite number: {0}", model.ComputeFavoriteNumber());
}

You don't even need Activator.CreateInstance in this case. 在这种情况下,您甚至不需要Activator.CreateInstance You do need it however, if you just saved the fully qualified name of the type or the type itself (not sure if that works) you want to create. 但是,如果您刚刚保存了要创建的类型的完全限定名称或类型本身(不确定是否可行),则确实需要它。

// interfaces/classes the same as above
Type deserializedType = typeof(ErnieModel); // or get it from wherever, maybe through (Type)bf.Deserialize(stream); ? In reality ErnieModel should be unknown to the deserializing code
var model = (IModel)Activator.CreateInstance(deserializedType);
Console.WriteLine("Favorite number: {0}", model.ComputeFavoriteNumber());

Using generics doesn't make sense in this case (though it feels like a good place to apply at first), you have to go with oldschool object and casting to a known interface type to enable true plugin-like extensions. 在这种情况下使用泛型是没有意义的(虽然它首先感觉是一个好地方),你必须使用oldschool object并转换为已知的接口类型以启用真正的插件式扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM