convert string data into Type for List<> - c#-4.0

I'd like to take a string and create a typed List based on the
"Type" documented in the string. For example, suppose str is "System.string". I'd
like the method to create a List for me. Of course, the string could contain text
that "references" any object in the assembly. I've unsuccessfully tried the following:
Type classType = Type.GetType(str);
List<classType> wgList = new List<classType>();
I get a message stating that "classType is a field but is used like a type"..
How do I fix this up to get what I need ?
The following code provided a nice solution:
Type ty = Type.GetType(ItemType);
wgList = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(ty));

You can not do that or i don't understand what is your purpose?
Use List< object > instead.
You may use Extension method is Cast< T>() in System.Linq namespace to convert your List< object > to List< T >.
List<Entry> entries = wgList.Cast<Entry>().ToList()

Related

IntelliJ Live Template for dart named constructor: lists class' fields

I want to generate a custom named constructor in Dart.
I have many dto class to implement and each should provide a named constructor like: ClassName.fromMap().
For example for this class:
class Student {
final String name;
final int age;
}
The generated constructor should be:
Student.fromMap(Map<String, dynamic> map) :
name = map['name'],
age = map['age'];
How can I retrieve the list of the field of my current class as strings? Is that even possibile?
Of course I can have a variable number of fields.
My template looks like:
$CLASS$.fromMap(Map<String, dynamic> map) :
$INITIALIZATION_LIST$
binding $CLASS$ to dartClassName().
Now I'd like to bind $INITIALIZATION_LIST$ to something like:
getClassFieldList().forEach((fieldName) => "$fieldName = map['$fieldName']")
Can I achieve something like that?
There is no way to retrieve a list of Dart class fields using predefined live template functions. You can try developing your own template macro for this. See https://intellij-support.jetbrains.com/hc/en-us/community/posts/206201699-create-a-new-expression-for-a-live-template-for-actionscript for some hints.
Existing live template functions implementations can be found at https://github.com/JetBrains/intellij-community/tree/master/platform/lang-impl/src/com/intellij/codeInsight/template/macro.
You can also try using Structural Search and Replace instead of live template

How to get type of the object name stored in a string variable?

I have an object name in a string variable. I need to find the type of the object. i could use LibraryExport function but i do not know which library has the object.
for example
String TheObj = "dw_grid_report"
How do i know either "dw_grid_report" is a window or a datawindow object?
FindClassDefinition (classname) is your friend.

Impex Export: Colon in multivalue attribute is escaped by double backslash - How to remove this behavior?

Hybris: 6.3.0.0-SNAPSHOT (the behavior is the same with 6.3.0.21)
When exporting impex, we noticed a difference when exporting a non-multivalue Type attribute versus exporting a multivalue Type attribute.
When exporting String attribute data without colon, a non-multivalue attribute can be exported as Experts, while a multivalue attribute can be exported as Experts|Hybris.
When exporting Type with String attribute data with colons (e.g. URL), the colon is escaped with a double backslash (for multivalue only). A non-multivalue attribute can be exported as https://experts.hybris.com, while a multivale attribute can be exported as https\://experts.hybris.com if there is only 1 value or as https\://experts.hybris.com|https\://help.hybris.com if there are 2 values.
How can I stop the export from escaping the colon? Is there a method I can override to change this behavior? I would like to change the result to https://experts.hybris.com|https://help.hybris.com or to "https://experts.hybris.com"|"https://help.hybris.com".
Business Case: We want to copy the URL from the exported impex, but the URL contains double backslashes. The exported impex is not meant to be reimported.
Notes #`: The URLs are stored in a collection (e.g. Product.newAttribute, where newAttribute is a collection of custom types which has a String). So, the Impex header looks something like "INSERT_UPDATE Product;newAttribute(data)"
Notes #2: (UPDATE: Didn't work) Currently, I'm checking if it's possible with a CSVCellDecorator; this is for import only.
Notes #3: Currently, I'm checking if it's possible with AbstractSpecialValueTranslator.
For this specific case, I created a new translator, extending AbstractValueTranslator. Then, I implemented the exportValue method, joining the string data (which are URLs), without escaping them.
public String exportValue(final Object value) throws JaloInvalidParameterException
{
String joinedString = "";
if (value instanceof Collection)
{
final Collection valueCollection = (Collection) value;
if (!valueCollection.isEmpty())
{
final ArrayList<CustomType> list = (ArrayList<CustomType>) valueCollection;
final StringJoiner joiner = new StringJoiner("|");
for (final CustomType customType : list)
{
// data is a URL
joiner.add(customType.getData());
}
// value would be something like "https://experts.hybris.com|https://help.hybris.com"
joinedString = joiner.toString();
}
}
return joinedString;
}
Reference:
Customization: https://help.hybris.com/1808/hcd/ef51040168d743879c015b7de232ce40.html
I think that might not be possible, since the colon is used to separate keys for referenced types. As in
...;catalogVersion(catalog(id),version);...
...;myCatalog:Staged;...
Why not run search/replace on the result?

Converting Object Names to Strings in AS2

So i've made an object called "potato", then started a loop to check if something in "_level0" has the name of "_level0.potato". What i think is that since the the things in _level0 are objects instead of strings, the object name can't be recognized as a string, so im guessing i need to find a way to convert the object name to a string or vice versa.
var potato:MovieClip = this.createEmptyMovieClip("potato", this.getNextHighestDepth());
for(objects in _level0){
trace(_level0[objects])
if(_root[objects] == "_level0.potato"){
trace("OMG, i found a potato on level0")
}
}
Your suggestion that the objects are stored as a string is incorrect. If you try using typeof before your
trace(typeof _level0[objects])
you will see that its type is movieclip
and your "_level0.potato" is string
They will not be equal. But you can convert object reference to string using String(...) construct.
And about names. You're confusing names and references. MovieClip object like some others in ac2 have property called _name. In this property name of object is stored like string. But only name, not the full path to its destination.
For your potato mc _name will be equal "potato"
So you could do your search like this
var potato:MovieClip = this.createEmptyMovieClip("potato",this.getNextHighestDepth());
for(objects in _level0){
trace(_level0[objects])
if(_root[objects]._name == "potato"){
trace("OMG, i found a potato on level0")
}
}

Automapper: Mapping a property value of an object to a string

Using Automapper, how do you handle the mapping of a property value on an object to an instance of a string. Basically I have a list of Role objects and I want to use Automapper to map the content of each "name" property to a corresponding list of string (so I just end up with a list of strings). I'm sure it has an obvious answer, but I can't find the mapping that I need to add to "CreateMap" to get it to work.
An example of the relevant code is shown below:
public class Role
{
public Guid Id{get;set;}
public string Name{get;set;}
...
...
}
// What goes in here?
Mapper.CreateMap<Role, string>().ForMember(....);
var allRoles = Mapper.Map<IList<Role>, IList<string>>(roles);
I love Automapper (and use it in a number of projects), but wouldn't this be easier with a simple LINQ statement?
var allRoles = from r in roles select r.Name
The AutoMapper way of accomplishing this:
Mapper.CreateMap<Role, String>().ConvertUsing(r => r.Name);

Resources