简体   繁体   中英

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". 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?)

that happends at casting the ser.Deserialize(sr);

You wanna create an instance of 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 ? 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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