我知道有很多主题都有同样的问题,但我很困惑。
我想遍历我的对象属性并写入值。
但我见过的所有内容都说
const clone = require('clone')
let db = {}
const defaultData = {
"8xf0y6ziyjabvozdd253nd": {
id: '8xf0y6ziyjabvozdd253nd',
timestamp: 1467166872634,
title: 'Udacity is the best place to learn React',
body: 'Everyone says so after all.',
author: 'thingtwo',
category: 'react',
voteScore: 6,
deleted: false,
commentCount: 2
},
"6ni6ok3ym7mf1p33lnez": {
id: '6ni6ok3ym7mf1p33lnez',
timestamp: 1468479767190,
title: 'Learn Redux in 10 minutes!',
body: 'Just kidding. It takes more than 10 minutes to learn technology.',
author: 'thingone',
category: 'redux',
voteScore: -5,
deleted: false,
commentCount: 0
}
}
function getData (token) {
let data = db[token]
if (data == null) {
data = db[token] = clone(defaultData)
}
return data
}
function getByCategory (token, category) {
return new Promise((res) => {
let posts = getData(token)
let keys = Object.keys(posts)
let filtered_keys = keys.filter(key => posts[key].category === category && !posts[key].deleted)
res(filtered_keys.map(key => posts[key]))
})
}
function get (token, id) {
return new Promise((res) => {
const posts = getData(token)
res(
posts[id].deleted
? {}
: posts[id]
)
})
}
function getAll (token) {
return new Promise((res) => {
const posts = getData(token)
let keys = Object.keys(posts)
let filtered_keys = keys.filter(key => !posts[key].deleted)
res(filtered_keys.map(key => posts[key]))
})
}
function add (token, post) {
return new Promise((res) => {
let posts = getData(token)
posts[post.id] = {
id: post.id,
timestamp: post.timestamp,
title: post.title,
body: post.body,
author: post.author,
category: post.category,
voteScore: 1,
deleted: false,
commentCount: 0
}
res(posts[post.id])
})
}
function vote (token, id, option) {
return new Promise((res) => {
let posts = getData(token)
post = posts[id]
switch(option) {
case "upVote":
post.voteScore = post.voteScore + 1
break
case "downVote":
post.voteScore = post.voteScore - 1
break
default:
console.log(`posts.vote received incorrect parameter: ${option}`)
}
res(post)
})
}
function disable (token, id) {
return new Promise((res) => {
let posts = getData(token)
posts[id].deleted = true
res(posts[id])
})
}
function edit (token, id, post) {
return new Promise((res) => {
let posts = getData(token)
for (prop in post) {
posts[id][prop] = post[prop]
}
res(posts[id])
})
}
function incrementCommentCounter(token, id, count) {
const data = getData(token)
if (data[id]) {
data[id].commentCount += count
}
}
module.exports = {
get,
getAll,
getByCategory,
add,
vote,
disable,
edit,
getAll,
incrementCommentCounter
}
这将创建一个包含属性信息的数组,但它不存储该object.property的实际值,
该物业的名称是自动收报机,但Mycompanies(1).ticker的价值是AMZN。
我的代码:
Dim pinfo() As PropertyInfo = MyCompanies(1).GetType().GetProperties()
注意:MyCompanies(1)只是对象数组中的第二个对象,所有对象都属于公司类
https://msdn.microsoft.com/en-us/library/b05d59ty(v=vs.110).aspx也没有帮助。这让我相信我应该这样做:
Dim pinfo() As PropertyInfo = MyCompanies(1).GetType().GetProperties()
'Loop through Properties
For pi = 0 To pinfo.Length - 1
'I want to get the values of each property
Console.WriteLine(?)
Next pi
但是,甚至不会建立。是因为我试图通过使用数组中的位置来赋予它对象吗?
...在这个循环之外我将不得不遍历我的对象数组,所以我想我可以用一个i替换1 ...
答案 0 :(得分:2)
你应该这样打电话:
Dim props As PropertyInfo() = MyCompanies(1).GetType().GetProperties()
注意GetProperties
返回一个PropertyInfo
数组 - 一个代表每个属性。
然后,循环:
For Each prop in props
Console.WriteLine(prop.GetValue(MyCompanies(1)).ToString())
Next
请注意,PropertyInfo类不包含对您从中派生类型信息的特定对象的引用(这就是您不能只调用prop.GetValue()
的原因)。
它只是一种描述有问题的类型的模板 - 因此你必须向它传递一个对你想要提取其属性值的实际对象的引用。
如果您正在访问多个MyCompanies对象,那么您只需获取一次类型信息,然后重复使用它。
例如:
Dim props As PropertyInfo() = MyCompanies(1).GetType().GetProperties()
For Each company in MyCompanies
Console.WriteLine(company.ToString()) 'e.g. to print the company name
For Each prop in props
Console.WriteLine(prop.GetValue(company).ToString())
Next
Next
我还没有对代码进行测试,所以可以原谅任何小问题。