开发者

Calling a class method dynamically

开发者 https://www.devze.com 2022-12-15 17:52 出处:网络
Edit: In real life i don\'t have a Book class. This is just an example to be clear. Real problem really needs reflection to solve it.

Edit: In real life i don't have a Book class. This is just an example to be clear. Real problem really needs reflection to solve it.

Suppose that I have some classes:

Book, Apple, Door.

class Book
{
   ...
   public decimal getPrice()
   {...}
   public string getTitle()
   {...}
   public decimal getAuthor()
   {...}
}

and something same for other cl开发者_运维技巧asses.

May i call a class method dynamically from a string:

Book myBook = new Book("Title", "Author", 44);

string title = runMethod(myBook, "getTitle");


You can do this via Reflection.

Book myBook = new Book("Title", "Author", 44); 

string title = (string) myBook.GetType().GetMethod("getTitle").Invoke(myBook, null); 


You can use something like this, using reflection:

class Program
{
static void Main(string[] args)
{
    var b = new Book("Book Title", 2342);

    Console.WriteLine(CallMethod(b, "GetTitle", "Not Found"));
}

public static K CallMethod<T,K>(T a, string method, K defaultOjb)
{
    var t = a.GetType();

    var mi = t.GetMethod(method);
    if (mi == null) return defaultOjb;

    var ret=mi.Invoke(a, new object[] {});

    return (K) ret;
}
}

public class Book
{
private readonly string _title;
private readonly decimal _price;

public decimal GetPrice()
{
    return _price;
}
public string GetTitle()
{
    return _title;
}

public Book(string title, decimal price)
{
    _title = title;
    _price = price;
}
}


Lookup reflection and MethodInfo. I believe that will lead you down the path you are looking for.


Previous answers are correct in mentioning reflection.

However, unless your true question is very different from your example it's unnecessary.

Judging from your example you could simply call it directly:

string title = mybook.getTitle();

If the point is that you don't know and don't want to care what specific object you're being given you can either use a base class, or an interface.

public interface IProduct
{
    string Name { get; }
    string Type { get; }
    float Price { get; }
}

Make your classes implement IProduct and you're guaranteed that your classes will have implemented the properties or functions you've required, and that they will be public no matter whether you're dealing with "Book", "Apple", or "Door".

    public void OutputProductsToConsole(List<IProduct> products)
    {
        for (int i = 0; i < products.Count; i++)
        {
            Console.WriteLine(products[i].Name + ": " + products[i].Price);
        }
    }


It is possible with reflection, but in the example, it would be simpler to just call the method directly:

string title = myBook.getTitle();


You are instantiating your new Book class with the property values. Are you not assigning the values so you can get them back later?

public class Book
{

    private string _title;
    private decimal _price;
    private string _author;

    public Book(string title, decimal price, string author);
    {
        _title = title;
        _price = price;
        _author = author;
    }

    public string Title
    {
        get
        {
            return _title;
        }
    }

    public decimal Price
    {
        get
        {
            return _price;
        }
    }

    public string Author
    {
        get
        {
            return _author;
        }
    }

}

Better yet, if these methods are common to all your classes create an interface and inherit that in your classes.

0

精彩评论

暂无评论...
验证码 换一张
取 消