Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by $$anonymous$$ · Sep 02, 2013 at 12:15 PM · c#parsetextasset

Fill variable if MemberInfo name is same as a string

I'm trying to build a fast system to fill variables on classes from a text file. I surely can do this in a simple way by reading certain lines on a text file, but I'm trying to do this dynamic as possible without the need to hardcode almost everything.

I use a static class to fill a Dictionary with key val strings that are taken from the text file (splitting each line), and I can get to the point to see if the text file have the variable names equal to the class variables names using MemberInfo, the problem is I don't know how to return back that variable as depending on the type I have to parse the string to int or float or anything else. This is what I've done so far on retrieving the text file strings:

 static public void GetVariableLine (string path, MemberInfo m) {
     
     string text = projectPath + "/" + path;
     
 using (StreamReader sr = new StreamReader(text)) {

     string line;
 
     while ((line = sr.ReadLine()) != null) {
         string[] newLine = line.Split('=');
         keyVal.Add(newLine[0], newLine[1]);
             
         foreach (MemberInfo member in m.GetType().GetMembers()) {
             if(member.Name == newLine[0])
                     Debug.Log (member.Name);
         }
             
         Debug.Log (newLine[1]);
     }
         
     sr.Close();
     keyVal.Clear();
         
     }
 }

And I call this function by the script I want the variables filled:

 void Start () {
     NFParser.GetVariableLine(file, this.GetType());
 }

So as you can see for now there are only debug logs in there, what I would like to do is to get back the variable from the dictionary for the corrisponding memberinfo name of the class, but I'm at a dead end, not even sure this is possible.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by ArkaneX · Sep 02, 2013 at 01:44 PM

Firstly - I believe there is an error in your method signature. You have

 MemberInfo m

and later you're calling

 m.GetType().GetMembers()

so I guess the parameter should be actual object instance.

Anyway, if I had to implement filling an object using values from file, I would do it this way:

 static Dictionary<string, string> keyVal = new Dictionary<string, string>();
 
 static public void FillObjectDataFromFile(string path, object instance)
 {
     string text = projectPath + "/" + path;
 
     var dicPropertyInfosByName = instance.GetType().GetProperties().ToDictionary(x => x.Name);
     var dicFieldInfosByName = instance.GetType().GetFields().ToDictionary(x => x.Name);
 
     keyVal.Clear();
     using (StreamReader sr = new StreamReader(text))
     {
         string line;
 
         while ((line = sr.ReadLine()) != null)
         {
             string[] newLine = line.Split('=');
             keyVal.Add(newLine[0], newLine[1]);
         }
     }
 
     foreach (var kv in keyVal)
     {
         if (dicFieldInfosByName.ContainsKey(kv.Key))
         {
             SetFieldValue(instance, dicFieldInfosByName[kv.Key], kv.Value);
         }
         else if (dicPropertyInfosByName.ContainsKey(kv.Key))
         {
             SetPropertyValue(instance, dicPropertyInfosByName[kv.Key], kv.Value);
         }
     }
 }
 
 private static void SetFieldValue(object instance, FieldInfo fieldInfo, string value)
 {
     fieldInfo.SetValue(instance, GetConvertedValue(value, fieldInfo.FieldType));
 }
 
 private static void SetPropertyValue(object instance, PropertyInfo propertyInfo, string value)
 {
     propertyInfo.SetValue(instance, GetConvertedValue(value, propertyInfo.PropertyType), null);
 }
 
 private static object GetConvertedValue(string value, Type type)
 {
     if (type == typeof(string) || type == typeof(object))
     {
         return value;
     }
     if (type == typeof(int))
     {
         return int.Parse(value);
     }
     if (type == typeof(float))
     {
         return float.Parse(value, NumberFormatInfo.InvariantInfo);
     }
     // if conditions for other types
 
     throw new Exception("Unknown type: " + type.FullName);
 }

This is of course some basic code, because it works for public properties/fields only, and is able to convert a few types only, but I think you should be able to expand it to fit your needs.

I hope I didn't misunderstand you, but if this is not the answer you expected, please comment :)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Sep 02, 2013 at 01:55 PM 0
Share

You understood perfectly what I'm trying to do, just a question I need a specific namespace for Type, as I'm getting back an error on this and is weird.

avatar image ArkaneX · Sep 02, 2013 at 02:04 PM 0
Share

Namespaces required for the above code:

 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Reflection;
 using System.Globalization;
avatar image $$anonymous$$ · Sep 02, 2013 at 02:06 PM 0
Share

Never$$anonymous$$d I stupiditly forgot to add System; By the way this work flawlesly, I will have only to add more types. Thank you.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Double Parse a text file into 2d array or jagged array 1 Answer

How to Convert FileInfo to TextAsset to add to List 1 Answer

FormatException: Input string was not in the correct format - headache 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges