3

I have class MyWord and many other classes such as Noun, Idiom etc. that inherits from MyWord.

I store those objects in one list:

List<MyWord> Dictionary;

When I Add new element to the list i do it like this:

var myWord = new MyWord(id, word, definition,type,DateTime.Now.ToShortDateString());
Dictionary.Add(myWord);

As you can see, I put there object MyWord. I would like to be able to create for example Noun object and put in into list, like this:

var myWord = new Noun(id, word, definition, type, DateTime.Now.ToShortDateString());
Dictionary.Add(myWord);

The type parameter is a string, for example "noun". I use this method to get Type based on this string:

    private Type GetFullType(string myType)
    {
        //Returns list of all types that inherit from MyWord class
        var supportedTypes = (AppDomain.CurrentDomain.GetAssemblies()
                            .SelectMany(assembly => assembly.GetTypes())
                            .Where(type => type.IsSubclassOf(typeof(MyWord)))).Select(x => x.FullName).ToList();
        return Type.GetType(supportedTypes.Find(x => x.Contains_SkipWhiteSpacesIgnoreCase(myType)));
    }

Now I don't know how to use the GetFullType() return value to convert MyWord to Noun. Like this:

        Type wordType = GetFullType(type);
        var myWord = new MyWord(id, word, definition, type, DateTime.Now.ToShortDateString());
        Dictionary.Add((wordType)myWord);

I hope I explained myself clearly.

Note that I could use If...else structure to create different objects checking the type variable but it's not an elegant solution. Any better ideas?

6
  • If you want to create a Noun instance based on the "Noun" string, then use your GetFullType method and create an instance with Activator.CreateInstance. Why would you want to convert a Noun to MyWord? A Noun is a MyWord. Commented Oct 24, 2015 at 15:11
  • I don't want to convert Noun to MyWord. I want to convert MyWord to Noun :P Commented Oct 24, 2015 at 15:36
  • Why? It looks like you need to work dynamically with a "Noun" string. The string can also be an "Idiom". Lets say I get the string "Noun" and I have a MyWord in my hand. What are the benefit's from converting the MyWord into a Noun in runtime? Commented Oct 24, 2015 at 15:43
  • Well I was hoping to create one list of MyWord elements, and populate it with Nouns,Idioms,Verbs etc. thanks to polymorphism. Am I perceiving polymormphism wrong? MyWord in fact can be anything, it's a generic word type in my case. In addition, in MyWord there is information stored what exactly type of word it is. Commented Oct 24, 2015 at 16:12
  • Creating a list of MyWords is ok and adding Nouns, Idioms into that list will work. Why would you want to convert a MyWord into a Noun? If you want to run a specific method on a Noun (which is a MyWord) you will need to cast it into a Noun before (or use reflection\dynamic). I don't really see your point here but I did see that you accepted an answer so I think your good. Commented Oct 24, 2015 at 17:06

2 Answers 2

3

You can use Activator.CreateInstance to create an instance of a type. See https://msdn.microsoft.com/en-us/library/wccyzw83(v=vs.110).aspx

var myword = Activator.CreateInstance(wordType);

And then you can set the properties on it using reflection, or by casting it to the base class MyWord.

OR you could use reflection on the wordType to find its constructor and call that.

BUT really, are there that many word types that it's not easier (and a whole lot faster) to maintain a Dictionary<string,Func<string, string, WordType>> where you can look up a word type and get a function that makes one for you from its word and definition values?

wordFactories["noun"](word, definition);

Another alternative is to use Impromptu.Interface from Nuget which can wrap any dynamic object in an interface and have it behave just like that. In my natural language engine for .NET that's what I use to take a word from a database and then wrap it up as, say, an INoun, an IPluralNoun, an IMammal, etc.

Sign up to request clarification or add additional context in comments.

2 Comments

I think I will go for the solution with Dictionary since you say that it's much faster. My program will later on read great number of words (like 200.000 or so) and reflecting that many objects may have performance issues, right?
For 200,000 you might also want to consider a better way to store the strings. I use a trie which uses a lot less memory.
1

What you try to do is still not possible, becuase if we take a look at this:

Type wordType = GetFullType(type);
var myWord = new MyWord(id, word, definition, type, DateTime.Now.ToShortDateString());
Dictionary.Add((wordType)myWord);

You create an instance of MyWord, which could not be casted in Noun or some thing like that, because it is no type of that. What you can use is Activate.CreateInstance for your purpose. Take a look at it here: Activator.CreateInstance

This static method also allows to take a list of parameter, which will automatically passed to the constructor. But be sure, this use reflection and is always slower than creating instances with the new keyword.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.