Question   I have a full name of a type, for example "StrategyA",and I want to get new Strategy(value) that assigned to its interface "IStrategy", how to get it?   What I have tries   I have tried this:  IStrategy strategy; if (strategyName == "StrategyA") {     strategy = new StrategyA(value); } else if(strategyName == "StrategyB") {     strategy = new StrategyB(date); } ...   but when I create a new strategy , I have to add another branch to the code, and I think it is a bad code style.  Is there any better way to solve this problem?
25435
Is there a way to create an instance of a class based on the fact I know the name of the class at runtime. Basically I would have the name of the class in a string.
80615
I am working on code generation and ran into a snag with generics. Here is a "simplified" version of what is causing me issues.  Dictionary&lt;string, DateTime&gt; dictionary = new Dictionary&lt;string, DateTime&gt;(); string text = dictionary.GetType().FullName;   With the above code snippet the value of text is as follows:   System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0,   Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.DateTime, mscorlib,   Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]   (Line breaks added for better readability.)  Is there a way to get the type name (type) in a different format without parsing the above string? I desire the following result for text:  System.Collections.Generic.Dictionary&lt;System.String, System.DateTime&gt;
114709
The title is pretty self-explanatory. I need something similar to , but for completely new entry types. (IIUC, the linked just shuffles one type onto another.)  I'm trying to define a @stackexchange type where I can cite StackExchange questions:  @stackexchange{se:l3help,   sitekey = {tex},   % sitetopic = {\TeX, \LaTeX, Con\TeX t, and related typesetting systems},   askid   = {45838},   title   = {What can \textit{I} do to help the \LaTeX3 Project},   askdate = {2012/02/26} }   where id must match the regexp [0-9]+, asked is a date, and sitekey provides a hard-coded mapping to the appropriate sitetopic when available. If sitekey isn't available, then sitetopic is required.  If sitetopic is supplied, then it overwrites the mapping from sitekey.  id, title, and asked are all required.  If there are any other fields you think should be included, feel free; I tried to pare down the data type only to the 'special' ones.  I would hope that this Q can cover most of the bases for different ways fields can act.  MWE (Adapted per comments)  \begin{filecontents}{references.bib} @stackexchange{se:l3help,     sitetopic = {tex},     title   = {What can \textit{I} do to help the \LaTeX3 Project},     askdate = {2012/02/26},     askid   = {45838},     askp    = {Brent Longorough},     askerid = 344,     ansp    = {Frank Mittelbach},     anspid  = 10109,     ansdate = {2012/03/01},     ansid   = 46427   } \end{filecontents} \begin{filecontents}{stackexchange.dbx} \DeclareDatamodelEntrytypes{stackexchange} \DeclareDatamodelFields{   sitekey,   sitetopic,   askdate,   askedit,   askid,   askp,   askpid,   ansp,   anspid,   ansid,   ansdate,   ansedit} \DeclareDatamodelEntryfields[stackexchange]{   sitekey,   sitetopic,   askdate,   askedit,   askid,   askp,   askpid,   ansp,   anspid,   ansid,   ansdate,   ansedit,   title} \end{filecontents} \documentclass{article}  \usepackage{csquotes} \usepackage[datamodel=stackexchange,backend=biber]{biblatex} \addbibresource{references.bib}  \newbibmacro*{stackexchangequestion}{%   \mkbibquote{\printfield{title}}   \mkbibparens{\printfield{askid}}   \addperiod }  \newbibmacro*{stackexchangeask}{%   Asked \mkbibdatelong{\printfield{askdate}}%     \iffieldundef{askedit}{}{ \mkbibparens{edited \mkbibdatelong{\printfield{askedit}}}}%   by   \printfield{askp} \mkbibparens{\printfield{askpid}}   \addperiod }  \newbibmacro*{stackexchangeans}{%   Answered \mkbibdatelong{\printfield{ansdate}}%     \iffieldundef{ansedit}{}{ \mkbibparens{edited \mkbibdatelong{\printfield{ansedit}}}}%   by   \printfield{ansp} \mkbibparens{\printfield{anspid}}   \addperiod }  \newbibmacro*{stackexchangesite}{%   \printfield{sitetopic}}  \DeclareBibliographyDriver{stackexchange}{%   \usebibmacro{bibindex}%   \usebibmacro{begentry}%   \usebibmacro{stackexchangequestion}%   \usebibmacro{stackexchangeask}%   \usebibmacro{stackexchangeans}%   \usebibmacro{stackexchangesite}%   \usebibmacro{finentry}}  \begin{document}  \cite{se:l3help}  \printbibliography \end{document}   Throws a Missing \endcsname error, likely caused by misuse of \usebibmacro.
53255
In my spring application context file, I have something like:  &lt;util:map id="someMap" map-class="java.util.HashMap" key-type="java.lang.String" value-type="java.lang.String"&gt;     &lt;entry key="some_key" value="some value" /&gt;     &lt;entry key="some_key_2" value="some value" /&gt;    &lt;/util:map&gt;   In java class, the implementation looks like:  private Map&lt;String, String&gt; someMap = new HashMap&lt;String, String&gt;(); someMap = (HashMap&lt;String, String&gt;)getApplicationContext().getBean("someMap");   In Eclipse, I see a warning that says:  Type safety: Unchecked cast from Object to HashMap  What did I do wrong? How do I resolve the issue?
25912
Is it possible for a generic interface's type to be based on a specific parent class?  For example:  public interface IGenericFace&lt;T : BaseClass&gt; { }   Obviously the above code doesn't work but if it did, what I'm trying to tell the compiler is that T must be a sub-class of BaseClass.  Can that be done, are there plans for it, etc.?  I think it would be useful in terms of a specific project, making sure a generic interface/class isn't used with unintended type(s) at compile time.  Or also to sort of self-document: show what kind of type is intended.
47989
Okay, we know that the following two lines are equivalent -    (0 == i) (i == 0)   Also, the first method was encouraged in the past because that would have allowed the compiler to give an error message if you accidentally used '=' instead of '=='.  My question is - in today's generation of pretty slick IDE's and intelligent compilers, do you still recommend the first method?    In particular, this question popped into my mind when I saw the following code -   if(DialogResult.OK == MessageBox.Show("Message")) ...    In my opinion, I would never recommend the above. Any second opinions?
38648
I found myself writing the following in a bit of technical documentation:     The Trainers' and Students' clients have very little in common, both user interface-wise and code-wise.   At first, I wondered whether I should have written user-interface-wise, user interface-wise, user interface wise or user interfacewise... or some other combination. A quick Google search  however that the "-wise" suffix actually isn't one you can or should use on any word.  What do you suggest? I guess the easiest way out is rephrasing (e.g., not only for what concerns the user interface, but also for actual methods), but I wanted to learn whether appending "-wise" to arbitrary words is incorrect, frowned upon or merely a stylistic choice.
80637
So I have a collection of objects.  The exact type isn't important.  From it I want to extract all the unique pairs of a pair of particular properties, thusly:  myObjectCollection.Select(item=&gt;new                                 {                                      Alpha = item.propOne,                                      Bravo = item.propTwo                                 }                  ).Distinct();   So my question is:  Will Distinct in this case use the default object equals (which will be useless to me, since each object is new) or can it be told to do a different equals (in this case, equal values of Alpha and Bravo => equal instances)?  Is there any way to achieve that result, if this doesn't do it?
96218
I came across this code and wanted others to provide their point of view... is it good or bad ? ;)  Class ReportClass {  public string ReportName {get; set;} }   Then it was used as follows in code:  displayReport(ReportClass.ReportName = cmbReportName.SelectedValue.ToString())   That is about the simplest form example I can give you. Quetion is... why can't I find examples ? What would this be called? Is this just asking for trouble?  EDIT: I'm referring to the inplace assignment. Which I wasn't aware of until today
63400
Seeing as C# can't switch on a Type (which I gather wasn't added as a special case because is relationships mean that more than one distinct case might apply), is there a better way to simulate switching on type other than this?  void Foo(object o) {     if (o is A)     {         ((A)o).Hop();     }     else if (o is B)     {         ((B)o).Skip();     }     else     {         throw new ArgumentException("Unexpected type: " + o.GetType());     } }
59203
I have a the following dictionary:  IDictionary&lt;int, IList&lt;MyClass&gt;&gt; myDictionary    and I am wanting to get all the values in the dictionary as an IList....    Just to add a bit of a background as to how I've gotten into this situation....  I have a method that gets me a list of MyClass. I then have another method that converts that list into a dictionary where they key is the id for MyClass. Later on...and without access to that original list...I'm needing to obtain the original ungrouped list of MyClass.    When I pass myDictionary.Values.ToList() to a method that takes an IList I get a compile error that says that it can't convert from   System.Collections.Generic.List&lt;System.Collections.Generic.IList&lt;MyClass&gt;&gt;    to:  System.Collections.Generic.IList&lt;MyClass&gt;   Now, I can understand that its gone and added each of the groups of IList to the new list as separate elements of the list....but in this instance its not really what I'm after. I just want a list of all the values in the entire dictionary.  How then can I get what I'm after without looping through each of the key values in the dictionary and creating the list I want?
31353
How do I create a resource that I can reference and use in various parts of my program easily?  My specific problem is that I have a NotifyIcon that I want to change the icon of depending on the state of the program. A common problem, but one I've been struggling with for a long time.
61444
I'm trying to build a Dynamic list(table) of records by selecting the Sobject and its fields.So I need to get all the fields with its API name,label,type.But I can't get the label and type.(Tried DescribeSObjectResult Class methods to get label,but it didn't help to get the label dynamically)  I tried it with the following code.  System.debug('Selection-----'+selectedObject); Map&lt;String, Schema.SObjectType&gt; gd = Schema.getGlobalDescribe();  Schema.SObjectType ctype = gd.get(selectedObject);  Map&lt;String, Schema.SobjectField&gt; fmap = ctype.getDescribe().fields.getMap();    List&lt;FieldWrap&gt; strList = new List&lt;WrapMap&gt;(); for(String s: fmap.keySet()) {     WrapMap wmp = new WrapMap();     wmp.name = s;     wmp.label = String.valueOf(fmap.get(s));     strList.add(wmp);  }   Where FieldWrap is my wrapper class with name and field of String types. I'm getting the List of API name of fields,but not the Label and type of it.Is there a way to get the label and type of field.If so, please help me.
85842
Assuming the following hypothetical inheritance hierarchy:  public interface IA {   int ID { get; set; } }  public interface IB : IA {   string Name { get; set; } }   Using reflection and making the following call:   typeof(IB).GetProperties(BindingFlags.Public | BindingFlags.Instance)    will only yield the properties of interface IB, which is "Name".   If we were to do a similar test on the following code,  public abstract class A {   public int ID { get; set; } }  public class B : A {   public string Name { get; set; } }   the call typeof(B).GetProperties(BindingFlags.Public | BindingFlags.Instance) will return an array of PropertyInfo objects for "ID" and "Name".  Is there an easy way to find all the properties in the inheritance hierarchy for interfaces as in the first example?
93253
I'm creating a generic class and in one of the methods I need to know the Class of the generic type currently in use. The reason is that one of the method's I call expects this as an argument.  Example:  public class MyGenericClass&lt;T&gt; {   public void doSomething() {     // Snip...     // Call to a 3rd party lib     T bean = (T)someObject.create(T.class);     // Snip...   } }   Clearly the example above doesn't work  and results in the following error: Illegal class literal for the type parameter T.  My question is: does someone know a good alternative or workaround for this?
1725
I have a generic method with this (dummy) code (yes I'm aware IList has predicates, but my code is not using IList but some other collection, anyway this is irrelevant for the question...)  static T FindThing&lt;T&gt;(IList collection, int id) where T : IThing, new() {     foreach T thing in collecion     {         if (thing.Id == id)             return thing;     }     return null;  // ERROR: Cannot convert null to type parameter 'T' because it could be a value type. Consider using 'default(T)' instead. }   This gives me a build error      "Cannot convert null to type parameter   'T' because it could be a value type.   Consider using 'default(T)' instead."   Can I avoid this error?
14961
This may be a generic OOP question. I wanted to do a generic comparison between an interface and an abstract class on the basis of their usage.   When would one want to use an interface and when would one want to use an abstract class?